I have added MouseMoveOverlay on OpenStreetMap but no mouse event are listened when i perform an event.
I want to add a Marker on map on double click. Here is my Main Code:
//creating map:
public JMap createMap() {
final JMap jMap = new JMap();
jMap.addMapOverlay(new MouseMoveOverlay());
ArrayList<String> tileServerURLs = new ArrayList<String>();
tileServerURLs.add("http://otile1.mqcdn.com/tiles/1.0.0/osm/");
tileServerURLs.add("http://otile2.mqcdn.com/tiles/1.0.0/osm/");
tileServerURLs.add("http://otile3.mqcdn.com/tiles/1.0.0/osm/");
tileServerURLs.add("http://otile4.mqcdn.com/tiles/1.0.0/osm/");
String attribution = "Data, imagery and map information provided by " +
"<a href=\"http://www.mapquest.com\">MapQuest</a>, " +
"<a href=\"http://www.openstreetmap.org\">OpenStreetMap.org</a> " +
"and contributors, " +
"<a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CCBYSA</a>";
// create and add the OpenStreetMapLayer
OpenStreetMapLayer osmLayer = new OpenStreetMapLayer(tileServerURLs, 0, 18, attribution);
// if a key is required to use the tiles, use the setKey method on the layer
//osmLayer.setKey("");
// set the extent
jMap.setExtent(new Envelope(-19856505, -8827900, 18574809, 16806021));
NavigatorOverlay navigatorOverlay = new NavigatorOverlay();
navigatorOverlay.setLocation(LocationOnMap.TOP_LEFT);
jMap.addMapOverlay(navigatorOverlay);
return jMap;
}
//Mouse Event
private class MouseMoveOverlay extends MapOverlay {
private static final long serialVersionUID = 1L;
@Override
public void onMouseClicked(MouseEvent event) {
try {
if (!map.isReady()) {
return;
} else {
if (event.getClickCount() == 2 && !event.isConsumed()) {
java.awt.Point screenPoint = event.getPoint();
com.esri.core.geometry.Point mapPoint = map.toMapPoint(
screenPoint.x, screenPoint.y);
double xx = Double.parseDouble(decimalFormat.format(mapPoint.getX()));
double yy = Double.parseDouble(decimalFormat.format(mapPoint.getY()));
map.setMarkerGraphicPopupsEnabled(true);
map.addMarkerGraphic(yy, xx, "House", "this is a house");
}
}
}
finally
{
super.onMouseClicked(event);
}
}
}
Hi, it looks like the issue with your code is to do with spatial references. The JMap.addMarkerGraphic method takes a latitude (y coordinate) then longitude (x coordinate) as the first two parameters. The OpenStreetMapLayer is in web mercator spatial reference (wkid 102100), with units in metres, not in latitude longitude. So you need to project your map points to the wgs 84 spatial reference (wkid 4326).
(Also your code snippet doesn;t actually add the osm layer to the map, but I assume your app does that, jMap.getLayers().add(osmLayer);)
Here's some code that worked for me, with projecting the map points (with some debug print statements):
@Override
public void onMouseClicked(MouseEvent event) {
System.out.println("mouse clicked");
try {
if (!map.isReady()) {
return;
}
if (event.getClickCount() == 2 && !event.isConsumed()) {
System.out.println("click count was 2");
com.esri.core.geometry.Point mapPoint = map.toMapPoint(event.getX(), event.getY());
com.esri.core.geometry.Point pointLatLon = (com.esri.core.geometry.Point) GeometryEngine.project(
mapPoint,
map.getSpatialReference(),
SpatialReference.create(SpatialReference.WKID_WGS84)); // or just use the int value, 4326
map.setMarkerGraphicPopupsEnabled(true);
double lat = pointLatLon.getY();
double lon = pointLatLon.getX();
System.out.println("lat: " + lat + ", lon: " + lon);
map.addMarkerGraphic(lat, lon, "House", "this is a house");
}
}
finally
{
super.onMouseClicked(event);
}
}
let me know how you get on,
~elise
Sir,
You are Amazing. You really solved a big problem. I am greatly thankful to you.
Best Regards,