1
votes

I am displaying markers on the map from geojson file. In current code when I hover over marker I can see the properties in the popup. I want to add fly to or zoom in marker exact location upon click on the marker.how can I achieve that.

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

2 Answers

1
votes

I have figured out the solution so, I am adding it here.

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

For a nice smooth animated pan/zoom effect rather than a jump, use flyTo

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