Recipe 7.6 Finding a User-Friendly Address Using Reverse Geocoding, and Vice Versa

Android Versions
Level 1 and above
Permissions
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
Source Code to Download from Wrox.com
Maps-Geocoding.zip

You want to determine the address of a location on the map simply by touching it. In addition, you want to be able to find the latitude and longitude of a given location.

Solution

While the users can pan the Google Maps to view the various locations graphically, it is useful to be able to touch the map and get the user-friendly address of the location being touched. To do so, you need to perform the following steps:

1. Get the latitude and longitude of the location being touched.
2. Perform a reverse geocoding operation to convert the latitude and longitude of a location into a user-friendly address.

The following sections show how to perform these operations using overlays in Google Maps.

Getting the Location That was Touched

To obtain the location that is being touched, you need to override the onTouchEvent() method within the Overlay class. This method is fired every time the user touches the map. This method has two parameters: MotionEvent and MapView. Using the MotionEvent parameter, you can determine whether the user has lifted his or her finger from the screen using the getAction() method.

In the following code snippet, you implement a class that extends ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.