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".
Related
I have a simple fragment with a mapview displayed. I have override all the necessary methods according to the documentation (destroy, resume and start) to cal the same method of the mapview. However when I detach the fragment or exit the application the location keeps on. It will drain too much battery. Is there any procedure I can make to stop the location feature?. I've tried calling mapview.onDestroy, mapview=null, etc etc and the location keeps on.
Thanks
Check How to unregister Listener & stop service from within broadcastreceiver. If you're using LocationManager to get location updates, call removeUpdates(getActivity()) on your LocationManager instance to stop them.
Edit as requested:
Searching the current location via GPS is disabled by calling setMyLocationEnabled(false) on the GoogleMap instance.
Android / location:
I would like to receive location updates every 5 seconds.
Should I call the method:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 0, gpsLocListener)?
It is important for me NOT to interfere with the osmdroid MyLocationManager functionality.
Or should I use the MyLocationOverlay.onLocationChanged(), e.g. by inheritance of the MyLocationOverlay class.
If so, how often will it be called?
There should be no problem having multiple listeners on the location manager. Each one will get events separately. In fact, if you wanted you could use a new instance of osmdroid's GpsMyLocationProvider to feed you location updates. Call mGpsMyLocationProvider.startLocationProvider(myLocationConsumer) where myLocationConsumer is an instance of IMyLocationConsumer that receives and handles gps updates as you wish. You can call mGpsMyLocationProvider.setLocationUpdateMinTime() to adjust how often you get updates.
I have registered proximity alert in one activity and want to update the coordinates from another activity. How do I do that? I have already tried creating an unregisterProximity method in the 1st activity but it doesn't let me make it static.
Any idea?
pass the coordinates from one activity to the other or use locationManager.removeProximityAlert(proximityIntent) with the same proximityIntent for when it was created
post code and add more info cause your question is not that clear
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.
I have used LastKnownLoaction at the start-up Activity of my app to get the location ,if it returns null then i register for a Location Listener for both gps and network providers.After requesting for Listener to listen for any location change i switch over to main Activity page using startActivity.Further in the startup Actyivity's onLocation in both Listener i am trying to get location and if any values found then i try to store them in preference and remove updated from both listener and notify the user about the location being updated.
Is this logic is correct way of coding ,do the listener listen for location update when its no longer on the top of the stack ,hence i am using a emulator i couldn't test gps functionality,i required your suggestions on this
thanks
You shouldn't register a LocationListener only if getLastKnownLocation() returns null, because that means the particular location provider is disabled. Also caching a real-time value like location doesn't sound like a good design. I would suggest to register/unregister LocationListener for each Activity in it's onStart/onStop methods.