0

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!

Elton Santos
  • 571
  • 6
  • 32
  • 1
    Possible duplicate of [leaflet layer.getbounds not a function](https://stackoverflow.com/questions/48954616/leaflet-layer-getbounds-not-a-function) – ghybs Jun 01 '18 at 05:17

1 Answers1

3

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/

Baptiste
  • 1,688
  • 1
  • 15
  • 25