Select to view content in your preferred language

ArcGIS Android - Get LatLon position of the graphic

6546
10
Jump to solution
04-24-2014 01:38 PM
UmaraniPandian
Occasional Contributor
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());         }     });

I'm trying to get the lat long position of the clicked marker. But the listener doesn't provide it. How can I get the position?
0 Kudos
1 Solution

Accepted Solutions
DanO_Neill
Deactivated User
Looks like the original code returns projected coordinates and you used GeometryEngine to convert to lat/lon.  If that is the correct position of your graphic than it sounds like your first question is solved as @sgillUS suggested.  Your second issue is caused by the fact that the return of the conversion to geographic cs is not a valid stop point on the network as it is in the pacific ocean and throwing the exception.

View solution in original post

0 Kudos
10 Replies
ShellyGill1
Esri Contributor
Hi,

From a Graphic, you can use getGeometry() - in your code here the Geometry should be a Point, because the addMarkerGraphic method takes X,Y the coordinates you passed in and turns this into a Point to use in the Graphic. It's best to check the Graphic.getGeometry() is a Point of course, just in case you end up with other types of graphics that have polygons or lines. Then from the Point you can use getX() and getY() to get coordinates - in your code it looks like your graphic was initially created with lat,long coordinates, so the X will represent the longitude and the Y will represent the latitude.

(Note that if you created your point initially with some other types of coordinate (e.g. the point is in a projected coordinate system) then you can use the GeometryEngine.project method to project the Point to use, for example, WGS 94 geographic coordinate system, and then get the X and Y from the projected point.)

Hope this helps,
0 Kudos
UmaraniPandian
Occasional Contributor
Hi Shelly,
             Thanks for the reply!
I tried this line of code and

calculateDirection((float)((Point)graphic.getGeometry()).getX(),(float)((Point)graphic.getGeometry()).getY());


calculateDirection(float x, float y){
    final Point loc = mapView.toMapPoint(x, y);
    Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    // Start building up routing parameters
                    RouteParameters rp = mRouteTask
                            .retrieveDefaultRouteTaskParameters();
                    NAFeaturesAsFeature rfaf = new NAFeaturesAsFeature();
                    // Convert point to EGS (decimal degrees)
                    Point p = (Point) GeometryEngine.project(loc, wm,
                            egs);
                    // Create the stop points (start at our location, go
                    // to pressed location)
                    StopGraphic point1 = new StopGraphic(mLocation);
                    StopGraphic point2 = new StopGraphic(p);
                    rfaf.setFeatures(new Graphic[] { point1, point2 });
                    rfaf.setCompressedRequest(true);
                    rp.setStops(rfaf);
                    // Set the routing service output SR to our mapView
                    // service's SR
                    rp.setOutSpatialReference(wm);

                    // Solve the route and use the results to update UI
                    // when received
                    mResults = mRouteTask.solve(rp);
                    mHandler.post(mUpdateResults);
                } catch (Exception e) {
                    mException = e;
                    mHandler.post(mUpdateResults);
                }
            }
        };
        // Start the operation
        t.start();
}


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.


Sometimes I dont get any response back and it loads infinitely.
0 Kudos
ShellyGill1
Esri Contributor
So are you successfully getting the X and Y / latitude and longitude of your Graphic now? If your first question is solved and you have a follow up question, it would be best if you post a new question on the forum - that way the title of your new post will hopefully find people who know about this second problem (I'm afraid I don't know the solution to this second problem). Also, when you post your follow up, it would be great if you post at which line of code you actually get the exception.
0 Kudos
UmaraniPandian
Occasional Contributor
I'm getting some values and those values doesn't seem to be right. That's why I'm getting error. Am I doing any thing wrong in getting the lat/lon values?
0 Kudos
ShellyGill1
Esri Contributor
I'd have to guess that there is nothing wrong with how you get the X and Y back from the geometry of the graphic - the geometry of a graphic does not change after it is created - so what were the X and Y values you got from the geometry, and what were the X and Y values of geometry when you created the Graphic initially? And what were you expecting the X and Y of the geometry to be?
0 Kudos
UmaraniPandian
Occasional Contributor
Hi Shelly,

The X and Y value which I get from the marker graphics is

x = -1.0401366E7
y = 5617207.0


and when this data is converted into map points using this line:

final SpatialReference wm = SpatialReference.create(102100);
final SpatialReference egs = SpatialReference.create(4326);
...
...
...
Point loc = mapView.toMapPoint(x, y);
Point p = (Point) GeometryEngine.project(loc, wm,
                            egs);


p.getX() = -139.79025608070413
p.getY() = 24.634571644245387
0 Kudos
DanO_Neill
Deactivated User
Looks like the original code returns projected coordinates and you used GeometryEngine to convert to lat/lon.  If that is the correct position of your graphic than it sounds like your first question is solved as @sgillUS suggested.  Your second issue is caused by the fact that the return of the conversion to geographic cs is not a valid stop point on the network as it is in the pacific ocean and throwing the exception.
0 Kudos
UmaraniPandian
Occasional Contributor
Hi Dano,

  That point was just an example. I tried to get lat long for many such points and I don't get a correct value. I'm not sure what mistake I'm doing.

The LatLong for the graphic added to the map : 44.881105, -93.203288
The X,Y value after execution the following code: -10375343.000000, 5602823.500000
Point temp = (Point)graphic.getGeometry();
0 Kudos
DanO_Neill
Deactivated User
Ok so you are able to get the lat/lon, you issue now is why your calculateDirection() method is throwing an exception?  Both points need to be in the same projection as your RouteParameters projection.  From your code it looks like your Map is in wm but your stop point representing your graphic is in egs

It does seem like your original question has been answered in so far as getting lat/lon from graphic.  I agree @sgillUS that you should start a new thread about your routing code if you continue to get this exception and we will work to resolve it from there.
0 Kudos