我有一个与leaflet地图相关的问题。我的地图上有一些标记,当点击其中一个时,标记被居中显示。现在,我希望不仅当点击一个标记时它能够居中,而且还希望能够将地图缩放到该标记位置。当我添加下面这段代码:
map.setZoom((16), {animate: true});
to
map.on('popupopen', function(centerMarker) {
var cM = map.project(centerMarker.popup._latlng);
cM.y -= centerMarker.popup._container.clientHeight/2
map.setZoom((16), {animate: true});
map.panTo(map.unproject(cM),{animate: true});
});
我的代码中心化并不完美,因为它会缩放地图,但无法将标记居中。但是如果我在期望的缩放级别(16)范围内,则所有其他标记都将居中。如果我在缩放级别16之外,我应该怎么做才能使地图缩放到标记并且标记也居中呢?我对leaflet和jquery都比较陌生...
autopan:false
。 - neogeomatcM.y -= centerMarker.popup._container.clientHeight/2
。它的作用是什么?你可能会对 http://leafletjs.com/reference.html#icon-popupanchor 感兴趣。 - neogeomat