简体   繁体   中英

openlayers marker moveTo only accurate at a specific zoom level

I have been working on a way for users to move a marker without dragging. Basically, the user clicks on the marker and it opens the info window bubble. In the bubble is a link to a javascript function that sets a click event on the map. When the user clicks somewhere on the map it is supposed to move the marker to the point clicked.

In my map, I have 18 zoom levels. At zoom level 15, this process works perfectly. If I zoom in AFTER clicking once, the marker still moves to exactly where I click. But then, if I refresh and start over at zoom level 16 and try to click somewhere, the marker is moved to a position higher and more to the left. Repeating this process at higher zoom levels, the marker is moved even further up and to the left on the map (in distance).

Doing the above at zoom levels lower than 15 work just fine as well.

Here's a snippet of the code:

lmLayer = new OpenLayers.Layer.Markers("Landmark Creation");
map.addLayer(lmLayer);
var marker = landmark['landmark_1234'];// this just pulls the marker out of storage
map.events.register("click", lmLayer, function(evt){
    var pixel = new OpenLayers.Pixel(evt.clientX,evt.clientY);
    marker.moveTo(pixel);
    OpenLayers.Event.stop(evt);
});

I have console logged out the clientX and clientY clicks and they do register the right x/y coordinates from left and top edges of the browser. But it seems that OL is miscalculating the moveTo at the zoom levels above 15.

Any ideas?

a little workaround while waiting to bug correction

lmLayer = new OpenLayers.Layer.Markers("Landmark Creation");
map.addLayer(lmLayer);
var marker = landmark['landmark_1234'];

map.events.register("click", lmLayer, function(evt){
    var pixel = new OpenLayers.Pixel(evt.clientX,evt.clientY);
    marker.lonlat = pixel;
    marker.moveTo(pixel);
    // workaround
    marker.draw();
    lmLayer.redraw();
    OpenLayers.Event.stop(evt);
});

Cheers, J.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM