onLocationChanged call the onDraw method in my custom view - android

I have a MapImageViewer Activity class that has methods for checking the phones gps location. This activity then calls a custom view of mine called MapCanvas - where the constructor takes the latitude and longitude and then draws a circle to the corresponding pixels on an image of a map.
This works ok, but I am wondering how can I update and call the onDraw method every time the gps coordinates change? I know it needs to go in the onLocationChanged method..but i'm not sure how i can pass the new latitude and longitude values from there to my Custom View class.

What about sending lat/lon from the onLocationChanged() method by using .sendBroadcast() and handle them inside your View class with a BroadcastReceiver?
Alternatively you could use a callback method from the LocationListener like shown in this post.

Call the view's invalidate() method every time you get new coordinates (or postInvalidate() if the locationListener is in a different thread from the UI thread)

Related

How to update periodically from service to activity in Android

I have somewhat experience with Android.I would like to know how to update periodically from service to activity in Android.I am playing with an app which has Location Service running at the background and update to Activity how much the user is traveling. In the past, I broadcast distance from service and I use broadcast Receiver in Activity. However, this method is way too slow.
I recently discover that I can bind service, or use Handler to update periodically to activity from service. However, I do not understand detail especially Handler. Can anyone explain me how can I update periodically from service to activity using Handler. When I use handler, do I need to bind service? Or are those separate thing.
What I want is I want to update how much user travel by using onlocationChange() method from service to activity which has UI.
I will first explain you how the onLocationChange(Location l) method works.
This method is call-back - it means it will be called by the android system automatically. It will be called A) after amount of time passes or B ) after your position changes by certain meters. Those two parameters are specified by you when you create the LocationManager.
Having this in mind, you should for example declare a global variable with the distance, lets say
int a = 0;
Now in this method you must use the Location l which you are passed. You must add the moved distance to the variable a. Now, if all this code is inside the activity where you display the distance traveled, you simply use the variable a, else you have many choices. Either put it in shared preferences, or use function that sends that to the other activity.
Best of luck.
Have you look at the official solution?
http://developer.android.com/training/location/receive-location-updates.html
also this is a good example for what you want
https://github.com/commonsguy/cwac-locpoll
I think this can help you.
First create one Interface to Update the location
Interface to get continuous updates.
public interface ILocationlistener {
public void updateLocation(Location location);
public void updateLatitude(Double lat);
public void updateLongitude(Double lang);
}
update the ILocationlistener interface in onLocationChanged(Location location) in LocationListener so every location changes you will get the updatats.
After Use ILocationListerner interfase to update location changes in Activity or Service. After getting location use Handler or UI thread to update the UI in main thread.
For example http://www.androidsourcehelp.com/2014/03/get-location-and-its-changes-in-android.html?m=1

Stop Location Updates after first time

I am using a service which implements LocationListener class.Now i want to know the current location(Latitude,Longitude) of the device. I have used requestLocationUpdates method so that onLocationChanged method gets called when the location changes. I want that onLocationChanged method should be called only once i.e. when i get the location for the first time, it should not be called again.
I have called stopSelf method to stop the service inside onLocationChanged method. But still i am getting location updates.
Please help me.
Call removeUpdates on LocationManager, passing your location listener.
mLocationManager.removeUpdates(mListener);

Fetching gps coordinates: continuously vs. single request

I have implemented a LocationListener activity to get the lat/lon coordinates. I launch a MapActivity and want to get the current location and show it on the map. Now I'm confused. What is the best approach to show a progress dialog while fetching the coordinates?
Start an activity for result (LocationListener) and show a progress dialog while waiting for the coordinates to be delivered.
Implement a getCoordinates() method in the LocationListener activity and just call it once i need it (in a thread?)
Include LocationListener into MapActivity and let it continuously update field vars from onLocationChanged()
Is there any better solution? I'm pretty sure there is, actually i don't really know what I'm doing right now... :-/
A good model for displaying location(s) on a map can be found on the android official guide. Check this, and scroll down to "Helping the user decide on where to go".

Android LocationManager in AsyncTask

Firstly, I know LocationManager doesn't have to be called in an AsyncTask as it's already non UI blocking :)
I have an activity which
1. Gets the users current location
2. Calls a webservice to retrieve a list of specific POIs around that location.
The user can choose to view the results as map or list using a TabActivity. Bearing this in mind the AsyncTask to get users location and call to webservice is managed by the TabActivity view rather than either of the docked views.
So I want the TabActivity to start an AsyncTask which first gets the users location, then calls the webservice. A progress dialog prevents switching views using the tabs during this process.
It's pretty much all working apart from getting the users location from the AsyncTask. The webservice code is written, the mapping and overlay code is written, the task progress dialog copes with orientation changes.
The focus on location is speed rather than accuracy. If the user chooses to view results on the mapview then I will provide a 'My location' button which will enable a more accurate location to be obtained. I just want to initially get a rough location and return the search results quickly.
Maybe I'm going about this the wrong way. Maybe I should display the map view, let the map activity find the current location, then call just the webservice in the async - but then what if the user taps the list view tab during the location phase ? I was also going to allow the user to specify their default view - some people may prefer a list to a map. In this case I would have a listview which had to retrieve the location.
Thanks for any advice
Martin.
I sussed it.
The problem I was having was that the LocationListener was not being called in the AsyncTask.
The reason for this was that although I'd created and prepared a lopper in the thread I hadn't called Looper.Loop()
I start the requestLocationUpdates, kick off a timer. Either the locationmanager responds with a location, or the timer expires. At this point I call looper.quit() to ensure things return to normal.
Seems to be working. You also have to remember to respond to the back button etc, cancelling the timer and looper if the AsyncTask is cancelled.

Triggering LocationListener's event manually

Is there a way to manually trigger the code inside a LocationListener's onLocationChanged method? It should receive a Location object with the last known location, I guess.
Sure thing, if it's your LocationListener - onLocationChanged() is a public method; just call it with whatever Location object you have on hand (maybe have your location listener cache it somewhere, or use LocationManager's getLastKnownLocation method to grab the last location seen from a specific provider and call onLocationChanged() with that).
You can use the setTestProviderLocation method of the LocationManager to mock new locations and have the onLocationChanged method of the registered listeners called when you want.
You should check the reference page. You also have an example here.

Categories

Resources