Skip To Content

Search for coordinates

In this topic

Reverse Geocoding

Reverse geocoding refers to the process of finding the nearest address to a geographic location. On Android devices, this is especially useful when processing information from the on-board GPS. You can take your current location and display useful information such as the closest address or point-of-interest.

Much like standard geocoding, reverse geocoding is exposed through the Locator class in the Android API. The required parameters are a Point geometry and a search distance, but input and output spatial references can be optionally specified. For more information on creating a Locator using an online service or local data, see the Search for places page.

When developing an Android application, you will more than likely not be hard-coding Points to reverse Geocode. The following snippet shows how to add reverse Geocoding to a MapView's OnTouchListener. It overrides the onSingleTap method of the MapView's onTouchListener to show addresses as you tap locations on the screen.

@Override
public boolean onSingleTap(MotionEvent point) {
 
    // Create the Locator if it hasn't been created yet
    if(mLocator == null)
      mLocator = Locator.createOnlineLocator();  
 
    // Add the touch point to the MapView
    Point mapPoint = mMapView.toMapPoint(point.getX(), point.getY());
    Graphic g = new Graphic(mapPoint, new SimpleMarkerSymbol(Color.BLUE, 10, STYLE.DIAMOND));
    mGraphics.addGraphic(g);
 
    try {
      // Attempt to reverse geocode the touch event   
      LocatorReverseGeocodeResult result = mLocator.reverseGeocode(mapPoint,50,
        mMapView.getSpatialReference(),mMapView.getSpatialReference());
 
      // Reverse geocoding results come back as [key,value] pairs, but you
      // can create a formatted address by iterating through and delimiting with spaces  
      Map<String,String> addressFields = result.getAddressFields();
      StringBuilder address = new StringBuilder();
      for(Entry<String,String> entry : addressFields.entrySet())
        address.append(entry.getValue() != null ? entry.getValue() + " " : "");
 
      // Show the address in the Map's callout    
      ((TextView) mCallout.findViewById(R.id.calloutText)).setText(address.toString());
      mMapView.getCallout().show(mapPoint, mCallout);
      mMapView.getCallout().setMaxWidth(700);        
 
    } catch (Exception e) {
      Log.e("Reverse Geocoding Error", e.getMessage());
    }
 
    return true;
}