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?