0

Here is the case:

Im creating a new marker on map:

centeredMarker = L.marker(map.layerPointToLatLng([mapCenterXPx, mapCenterYPx]), markerOptions).addTo(map);

And then it should center and stay fixed on view center of map when map dragging by user. Here is my implementation:

map.addEventListener('move', toCenterMarker) 

where toCenterMarker function is

function toCenterMarker() {
  let mapCenterPx = map.latLngToLayerPoint([map.getCenter().lat, map.getCenter().lng]);
  let mapCenterXPx = mapCenterPx.x;
  let mapCenterYPx = mapCenterPx.y - 80;
  let mapCenter = map.layerPointToLatLng([mapCenterXPx, mapCenterYPx]);
  centeredMarker.setLatLng(mapCenter);
}

So the problem is when user dragging map, marker fixed on center and thats feels fine, but user can see that this marker have a little shakes on moving. Is there any solution of this problem?

1
  • If you want marker to be independent of the lat,lng position of the point below it, i.e. it has static position in view frame, than it makes no sense to tie it to some lat,lng coordinate and then modify it's position when map moves. Just create an HTML element with absolute position and put your marker image inside it. – TomazicM Apr 7 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.