J. Sveum
J. Sveum

Reputation: 29

Leaflet/mapbox: Geolocation zoom

I'm using Geolocation on my map and I can't figure out how to set the zoom level when gelocation is found. My code looks like this:

if (!navigator.geolocation) {
    geolocate.innerHTML = 'Geolocation is not available';
} else {
    geolocate.onclick = function (e) {
        var tl = new TimelineMax({repeat:2});   
        tl.to("svg.pointwhite", .10, {transformOrigin: "50% 100%", scaleY:0.25, yoyo:true, repeat:1})
          .to("svg.pointwhite", .65, {y:-6, ease:Circ.easeOut, yoyo:true, repeat:1});                   
        e.preventDefault();
        e.stopPropagation();
        map.locate();           
    };
}  

Is it inside the map.locate you set the zoom level? I have tried it of course and failed. As always, I really appreciate any help you guys can provide.

Upvotes: 1

Views: 499

Answers (2)

CCantey
CCantey

Reputation: 306

When using Leaflet I use map.setView(). Here is my Leaflet navigator code:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
  var pos = {
    latlng: {lat:position.coords.latitude,lng:position.coords.longitude},
    lat:position.coords.latitude,
    lng:position.coords.longitude
  };

  addMarker(pos);
  map.setView(L.latLng(pos.lat, pos.lng),13);

});
} else {
  // Browser doesn't support Geolocation
  handleLocationError(false, infoWindow, map.getCenter());
}

Upvotes: 1

Christoph
Christoph

Reputation: 1630

See this link.

When using the locate() method, you can give it an Locate options object. This can contain a maxZoom property:

map.locate({maxZoom: 18});

But it seems like you can't specify a kind of minZoom

Upvotes: 0

Related Questions