Geocoding your location found android eclipse
My code currently displays the users location on a map... using the code below
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.runOnFirstFix(new Runnable() {
public void ru开发者_Python百科n() {
controller.setZoom(8);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
I want to use the current location of the user and perform geocding on it to display the address as a toast message.. Would i have to delete the code above and retrieve the location by separate longitude and latitude values? Preferably i wouldnt want to go changing the code above as it already works.. I can't seem to find any good tutorials out there.. Could someone please direct me to one? Thanks
Since
MyLocationOverlay
already implementsLocationListener
, you could simply subclass it and override theonLocationChanged
listener method and perform your geocoding there (preferably in a new thread/async task to avoid blocking the overlay processing!).Something like:
private void initMyLocation() { final MyLocationOverlay overlay = new MyLocationOverlay(this, map){ public void onLocationChanged(android.location.Location location) { //subclass AsyncTask to take a location parameter for //doInBackground where it geocodes and then passes a String //to onPostExecute where you can display a toast new MyAsyncTask().execute(location); //remember to call the parent implementation to avoid breaking //MyLocationOverlay default functionality super.onLocationChanged(location); } }; overlay.enableMyLocation(); overlay.runOnFirstFix(new Runnable() { public void run() { controller.setZoom(8); controller.animateTo(overlay.getMyLocation()); } }); map.getOverlays().add(overlay); }
The other option might be to write a custom
LocationListener
and register for updates for location and do pretty much the same thing as in the code above. IMO, this is probably redundant and unnecessarily expensive since, as explained above, you already have aLocationListener
implementation to hook into so why write another?
精彩评论