First, try setting the distance to 50 locator.reverseGeocode(mapPoint, 50);
.
The PlaceSearch sample already does the specific feature you are trying to implement. The current implementation geocodes the address, or place, in the edit text field after you hit the Go button. Then when you click on the blue graphic representing the result a callout appears with the gecode result. Look at the setOnSingleTapListener
method on the MapView
to see this implementation.
Here is a simple workflow to implement reverse geocoding which returns an address to an Android Toast anywhere on the map.
Create an AsyncTask to Reverse Geocode off the UI thread
private class ReverseGeocodeTask extends
AsyncTask {
@Override
protected LocatorReverseGeocodeResult doInBackground(Point... params) {
// create results object and set to null
LocatorReverseGeocodeResult result = null;
// set the geocode service
locator = new Locator();
try {
// Attempt to reverse geocode the point.
// Our input and output spatial reference will be the same as the map.
SpatialReference mapRef = mMapView.getSpatialReference();
result = locator.reverseGeocode(params[0], 50,
mapRef,
mapRef);
} catch (Exception e) {
e.printStackTrace();
}
// return the resulting point(s)
return result;
}
protected void onPostExecute(LocatorReverseGeocodeResult result) {
String resultAddress = "";
// Construct a nicely formatted address from the results
StringBuilder address = new StringBuilder();
if (result != null && result.getAddressFields() != null) {
Map addressFields = result.getAddressFields();
address.append(String.format("%s\n%s, %s %s",
addressFields.get("Address"), addressFields.get("City"),
addressFields.get("Region"), addressFields.get("Postal")));
// Show the results of the reverse geocoding in a toast.
resultAddress = address.toString();
Toast toast = Toast.makeText(PlaceSearchActivity.this,
resultAddress, Toast.LENGTH_LONG);
toast.show();
}
}
}
Then implement setOnLongPressListener
on the MapView onCreate
method
mMapView.setOnLongPressListener(new OnLongPressListener() {
private static final long serialVersionUID = 1L;
@Override
public void onLongPress(float x, float y) {
Point mapPoint = mMapView.toMapPoint(x, y);
new ReverseGeocodeTask().execute(mapPoint);
}
});
You can find more reference on Reverse geocoding from the ArcGIS Online Geocoding Service here.