1

I'm adding marker to my map using the following code:

mvHelper.addMarkerGraphic(Double.parseDouble(friendData.getLatitude()), Double.parseDouble(friendData.getLongitude()), friendData.getFriendName(), friendData.getStatus(), null, icon, false,
                0);

This is my listener:

mvHelper.setOnGraphicClickListener(new OnGraphicClickListener() {
        @Override
        public void onGraphicClick(Graphic graphic) {
            Log.d("MVHELPER GRAPHIC LISTENER TAG",graphic.toString());
            calculateDirection((float)((Point)graphic.getGeometry()).getX(),(float)((Point)graphic.getGeometry()).getY());
        }
    });  

I'm trying to get the lat long position of the clicked marker. But the listener doesn't provide it. So I tried the above way and got this error:

com.esri.core.io.EsriServiceException:Unable to complete operation. Location "Location 2" in "Stops" is unlocated. Need atleast 2 valid stops. "Stops" does not contain valid input for any route.

and this is the method where I get the route:

calculateDirection(float x, float y){
Log.d("Marker X and Y",String.format("%f // %f",x,y));
final Point loc = mapView.toMapPoint(x, y);
Log.d("Marker X and Y in MapPoint",String.format("%f // %f",loc.getX(),loc.getY()));
Thread t = new Thread() {
        @Override
        public void run() {
            try {

                RouteParameters rp = mRouteTask
                        .retrieveDefaultRouteTaskParameters();
                NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();

                Point p = (Point) GeometryEngine.project(loc, wm,
                        egs);
                Log.d("Marker X and Y after projection",String.format("%f // %f",p.getX(),p.getY()));
                StopGraphic point1 = new StopGraphic(mLocation);
                StopGraphic point2 = new StopGraphic(p);
                rfaf.setFeatures(new Graphic[] { point1, point2 });
                rfaf.setCompressedRequest(true);
                rp.setStops(rfaf);

                rp.setOutSpatialReference(wm);

                mResults = mRouteTask.solve(rp);
                mHandler.post(mUpdateResults);
            } catch (Exception e) {
                mException = e;
                mHandler.post(mUpdateResults);
            }
        }
    };

    t.start();

}

Update 1:

After trying @jdONeill answer. I get the Lat and Long values.This is what I did.

Point temp = (Point)graphic.getGeometry();

Point p = (Point) GeometryEngine.project(temp, wm,egs);
calculateDirection((float) p.getX(), (float) p.getY());

This is the result:

04-30 16:17:54.408  17908-17908/com.test.esrisample D/Marker X and Y﹕ -93.203285 // 44.881104
04-30 16:17:54.408  17908-17908/com.test.esrisample D/Marker X and Y in MapPoint﹕ -10379311.931913 // 5607101.545301
04-30 16:17:54.458  17908-17908/com.test.esrisample D/dalvikvm﹕ GC_FOR_ALLOC freed 484K, 22% free 10018K/12760K, paused 16ms, total 16ms
04-30 16:17:54.719  17908-18235/com.test.esrisample D/Marker X and Y after projection﹕ -93.238945 // 44.908331

Why is there a slight change in the latitude and longitude values?

1 Answer 1

2

Get the geometry from the Graphic with getGeometry(). If Geometry.isPoint() returns true you can get the X and Y coordinate of the point with Point.getX() & Point.getY() respectively. To get a lat/lon you will need to ensure you have the correct projection. Your route code uses the GeometryEngine to reproject the point so after your conversion you should have the lat/lon of the point graphic.

As to the exception you are getting, you must ensure the following:

  • Both points are on a network to route
  • Both points are in the same projection

From your code it looks like your Map is in wm but your stop point representing your graphic is in egs.

1
  • This helped. You are right. Once I projected the point which I get from the Graphic to egs I'm getting the lat long values. But still there is a slight difference between the original lat long position and the one received after projecting it in mapPoint. Please check the update in the question. Commented Apr 30, 2014 at 21:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.