[GIS] Reverse Geocode for Android

androidarcgis-androidreverse-geocoding

I am new to arcgis for android. I practice using Geocode and reverse geocode. I use sample Place Search from Esri, but i also want to add Reverse Geocode in it so that after a user put in the address and search, we put a circle graphic on the map. And if a user tap on the circle, a toast should show the street name.
I have a void method called identityLocation(float x, float y). Inside that method i have:

Point aPoint = new Point(x,y);
Point mapPoint = (Point) GeometryEngine.project(aPoint, SpatialReference.create(4326), mapView.getSpatialReference());

How do return the Street name? I tried to use: locator.reverseGeocode(mapPoint, 5.0) but i was unable to.
Please help me. Thanks

Best Answer

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.