首页 > 解决方案 > 单击时缩放到标记位置 - 传单

问题描述

我正在地图上显示来自 geojson 文件的标记。在当前代码中,当我将鼠标悬停在标记上时,我可以在弹出窗口中看到属性。我想在单击标记时添加飞行或放大标记的确切位置。我该如何实现。

 cityMarker = new L.geoJson(city, {
        onEachFeature: function(feature, layer) {
            //if (feature.properties && feature.properties.name) {
            if ( feature.properties.name) {   
                layer.bindPopup(feature.properties.name, {closeButton: false, offset: L.point(0, -2)});
                layer.on('mouseover', function() { layer.openPopup(); });
                layer.on('mouseout', function() { layer.closePopup(); });
            }
        },
        pointToLayer: function (feature, latlng) {
            var cityIcon = new L.Icon({
            iconSize: [20, 20],
            iconAnchor: [13, 27],
            popupAnchor: [1, -20],
            iconUrl: './css/img/marker-icon-red.png'
        });
            //return L.circleMarker(latlng);
            return L.marker(latlng,{icon: cityIcon});
        }
    });
  
    map.addLayer(cityMarker);

标签: javascripthtmlleafletzoomingmarker

解决方案


要获得平滑的动画平移/缩放效果而不是跳跃,请使用flyTo

cityMarker.on('click', function(e) {
      map.flyTo(e.latlng, 16);      
});         

推荐阅读