Elton Santos
Elton Santos

Reputation: 581

Zoom on click in marker using leaflet for more then actions

I have a problem when I try make a zoom on marker, I got a error:

Uncaught TypeError: e.target.getBounds is not a function

var rotas = L.geoJSON(paradas, {
    onEachFeature: onEachFeature
}).addTo(map);

function onEachFeature(feature, layer){
    layer.on('click', function(e){
        $('.orange').html(feature.properties.nome);
        $('.city').html(feature.properties.imagem);
        $('.event').html(feature.properties.descricao);

        console.log(e.target);
        zoomToFeature(e)
    });

}

function zoomToFeature(e) {
    console.log("pass here")
    map.fitBounds(e.target.getBounds());
}

But when I do console.log it return correctly. What I'm wrong? My source code is here:

http://github.com/eltonsantos/analise_integrada

the map.fitBounds appear is ok, but still dont work :( Someone help me? thanks!

Upvotes: 0

Views: 2151

Answers (1)

Baptiste
Baptiste

Reputation: 1785

You have to do something else for a single marker

function zoomToFeature(e)
{
  var latLngs = [e.target.getLatLng()];
  var markerBounds = L.latLngBounds(latLngs);
  map.fitBounds(markerBounds);
}

First get the marker latlng array and create a latLngBounds with it. You can then fit on this bounds.

Working example: https://jsfiddle.net/8282emwn/175/

Upvotes: 3

Related Questions