Search code examples
androidgeolocationgpslocationlistenergoogle-maps-android-api-2

Android Google Maps Api V2, GPS locationlistener and geocode


NOTE: this question has nothing to do with MapView, MapViewActivity or GeoPoints, that api has been deprecated

Hello,

I'm having trouble with the android google maps api v2. Specifically I want make sure that the map pans when my location moves, and I want to listen to these events so I can get the string address of that location, as well as the longitude and latitude.

I am sure to get this once right now, but I want to listen to the changes.

My problem is that I am just not sure how much the new map does and what the LocationSource object will do for me. The sample application does not use GPS at all.

I'm not sure if I need to implement LocationListener to access the GPS hardware.

I need to be able to pan the map and get the geocoded address on the fly (by running the geocode method after its been detected that the map moved)

Does anyone have working geocoding using android google maps v2? I'm currently:

a) not sure if LocationSource is actually listening to movement changes

b) how to get the movement changes into my map object to update the screen, coordinates, and run geocoding


Solution

  • a) not sure if LocationSource is actually listening to movement changes

    You are the source of locations if you are using setLocationSource(). You will be passed an OnLocationChangedListener in your activate() method (part of the LocationSource interface). You need to call onLocationChanged() on that OnLocationChangedListener when you get a new location fix, by whatever means you choose (e.g., GPS).

    b) how to get the movement changes into my map object to update the screen, coordinates, and run geocoding

    By calling onLocationChanged() on that OnLocationChangedListener, the GoogleMap will be informed about the new location and, if that location is presently visible on the map, will mark it, assuming that you called setMyLocationEnabled(true) on the GoogleMap. Or, you can update the position of a Marker to reflect the user's position yourself.

    Geocoding is completely independent of GoogleMap. When you get location fixes from whatever means that you choose, you can also run through your geocoding logic in addition to updating the map.