0

I'm trying to write reverse geocoding. And i copied code given here... Here is my code sample..

      public class MagProActivity extends Activity {

            private MapView mMapView;
            private GraphicsLayer locationLayer; 


            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.main);

                mMapView = (MapView) findViewById(R.id.map);
                mMapView.addLayer(new ArcGISDynamicMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));

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 ReverseGeocoding(MagProActivity.this, mMapView).execute(mapPoint);

            }
        });

      }
    }

ReverseGeocoding class

import java.util.Map;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.Toast;

import com.esri.android.map.MapView;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.tasks.ags.geocode.Locator;
import com.esri.core.tasks.ags.geocode.LocatorReverseGeocodeResult;

public class ReverseGeocoding extends AsyncTask<Point, Void, LocatorReverseGeocodeResult>{

    private Activity curActivity;
    private MapView mapView;

    public ReverseGeocoding(Activity _curActivity, MapView _mapView){
        this.curActivity = _curActivity;
        this.mapView = _mapView;
    }

    @Override
    protected LocatorReverseGeocodeResult doInBackground(Point... params) {
        // create results object and set to null
        LocatorReverseGeocodeResult result = null;
        // set the geocode service
        Locator 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 = mapView.getSpatialReference();
            result = locator.reverseGeocode(params[0], 50.0, 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("Street"), addressFields.get("City"),
                    addressFields.get("State"), addressFields.get("ZIP")));

            // Show the results of the reverse geocoding in a toast.
            resultAddress = address.toString();
            Toast.makeText(curActivity, resultAddress, Toast.LENGTH_LONG).show();
        }
    }

}

When i long click on mapview this error will come com.esri.core.io.EsriServiceException: Cannot perform query. Invalid query parameters and toast will show all parameters null except city.. I think i'm doing some mess with Tiled service on Dynamic service.. Please help what i did wrong..

Thanks in Advance

1 Answer 1

1

The issue is in the return from the LocatorReverseGeocodeResult. Make the following edit to the address string:

address.append(String.format("%s\n%s, %s %s",
                addressFields.get("Address"), addressFields.get("City"),
                addressFields.get("Region"), addressFields.get("Postal")));

More information about what is returned from request can be found here. I will update the post you referenced.

1
  • Thank you It worked for me... Am I doing any mess with Tiled layer and Dynamic layers, I don't know which sample service to use for Dynamic and Tiled layer Please suggest nay site to refer.. Commented Aug 3, 2013 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.