[OpenLayers-Users] openlayer markers located at the wrong place

Mario Danelli mario.danelli at gmail.com
Fri May 31 01:25:33 PDT 2013


Dear Maven,

> As you can see, once I click on the map, I want the marker's position
updated and re-located.

As you can see, from the following link too

http://stackoverflow.com/questions/9278174/how-to-move-a-marker-in-an-openlayers-layer-markers-layer

It seems that the "moveTo" -- is not "official" APIMethod -- or in any case
there are some problems with it.

So I tried removing and adding a new marker at any click on map.
Below the JS code, that runs as expected, I tried in your example.
As you can see I changed the code of "updateMaker" function.

---------> BEGIN

$(window).load(function() {            
        var map = new OpenLayers.Map("map");
        map.addControl(new OpenLayers.Control.MousePosition());
        var osm = new OpenLayers.Layer.OSM();
        map.addLayer(osm);
        // Set view to zoom maximum map extent
        map.zoomToMaxExtent();

		var markers = new OpenLayers.Layer.Markers("Markers");
		map.addLayer(markers);
        
        myMarker = new OpenLayers.Marker(new OpenLayers.LonLat(0,0));
		markers.addMarker(myMarker);

		map.events.register("click", map, function(evt) {
			var pos = map.getLonLatFromPixel(evt.xy);
			updateMaker(myMarker, pos);
		});

		function updateMaker(myMarkerIn, pos) {
			  //myMarker.moveTo(myMarker.map.getPixelFromLonLat(pos));
		   markers.removeMarker(myMarkerIn);  
           myMarkerIn.destroy();
           myMarker = new OpenLayers.Marker(pos);
		   markers.addMarker(myMarker);
		}
	});

<--------- END

I hope it can be useful.

Regards

Mario Danelli



More information about the Users mailing list