Zoom sulla posizione dell'indicatore al clic - Opuscolo

Aug 20 2020

Sto visualizzando i marcatori sulla mappa dal file geojson. Nel codice corrente quando passo il mouse sopra il marker posso vedere le proprietà nel popup. Voglio aggiungere il volo o ingrandire la posizione esatta dell'indicatore dopo aver fatto clic sull'indicatore. Come posso ottenerlo.

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

Risposte

1 poonampatel Aug 20 2020 at 13:31

Ho capito la soluzione, quindi la aggiungo qui.

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

Per un piacevole effetto pan/zoom animato piuttosto che un salto, usaflyTo

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