クリックでマーカーの位置にズーム-リーフレット

Aug 20 2020

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);

回答

1 poonampatel Aug 20 2020 at 13:31

私は解決策を見つけたので、ここに追加します。

 cityMarker.on('click', function(e) {
      map.setView(e.latlng, 16);      
});         
1 peeebeee Aug 20 2020 at 14:20

ジャンプではなく、滑らかなアニメーションのパン/ズーム効果を得るには、次を使用します。 flyTo

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