This is my click listener on the map:
@Override
public boolean onSingleTap(MotionEvent point) {
Point mapPoint=view.toMapPoint(point.getX(), point.getY());
SpatialReference sp = SpatialReference.create(SpatialReference.WKID_WGS84);
Point p1Done=(Point) GeometryEngine.project(mapPoint, view.getSpatialReference(), sp);
UtilsMapa.addPoint(p1Done, view, "Marker",view.getGraphicLayer());
return super.onSingleTap(point);
}
The addPoint method:
public static void addPoint(Point point, MapViewExt mView, String textMarker, GraphicsLayer gLayer){
SimpleMarkerSymbol simpleMarker = new SimpleMarkerSymbol(Color.RED, 10, SimpleMarkerSymbol.STYLE.CIRCLE);
TextSymbol bassRockTextSymbol = new TextSymbol(10, textMarker, BLUE, TextSymbol.HorizontalAlignment.LEFT,
TextSymbol.VerticalAlignment.BOTTOM);
//When reaching this line, point has 0=-3.0246720297389555 1=40.83564363734672, which is where I tapped the screen
Graphic graphic=new Graphic(point,simpleMarker);
gLayer.addGraphic(graphic);
}
The graphic IS added (a red spot) but it is drawn at 0,0 coords... Why is that? How can I get the point drawn in the proper place?
Thank you.
Don't project the point into WGS 1984. Use the point that MapView.toMapPoint(double, double)
returns to you.
In ArcGIS Runtime 10.2.x for Android, a Point
object doesn't keep track of its spatial reference. It's really just an x-value and a y-value. If you give a Point
to the MapView
, the MapView
will assume that the Point
is in the same spatial reference as the map, which by default is usually in Web Mercator.
Here's what your code is doing:
Point mapPoint=view.toMapPoint(point.getX(), point.getY());
mapPoint
is now a point in MapView
's spatial reference, which is probably Web Mercator. The point is probably some number of millions of meters from (0, 0), but since the Point
class is blissfully unaware of spatial references, it just contains an x-value in the millions and a y-value in the millions, with no units.
SpatialReference sp = SpatialReference.create(SpatialReference.WKID_WGS84);
Point p1Done=(Point) GeometryEngine.project(mapPoint, view.getSpatialReference(), sp);
p1Done
is now a point in WGS 1984, which is some number of degrees from (0, 0). You have a comment that says the x-value is -3.0246720297389555 and the y-value is 40.83564363734672, or maybe the other way around, but the important point is that the x-value is between -180 and 180 and the y-value is between -90 and 90, with no units.
UtilsMapa.addPoint(p1Done, view, "Marker",view.getGraphicLayer());
Your helper method is called with p1Done
, which is called point
in your helper method.
Graphic graphic=new Graphic(point,simpleMarker);
A graphic is created with a point whose x-value is -3.0246720297389555 and y-value is 40.83564363734672.
gLayer.addGraphic(graphic);
The graphic is added to a graphics layer, which has the spatial reference of the map that contains it, which is probably Web Mercator. The map correctly displays the point -3.024672029738955 meters west of (0, 0) and 40.83564363734672 meters north of (0, 0).
If you had used Runtime 100.x, you might have avoided this problem, since Runtime 100.x geometries, including Point
, do keep track of their spatial reference.