0

I have the following that prompts the user to give location access. On granting permission the user's location is shown and the map loads. I'm trying to find a solution for if the user accidentally denies location access. Can anyone advise?

function onLocationFound(e) {
  var radius = e.accuracy / 4;

  L.circle(e.latlng, radius).addTo(map);
}

function onLocationError(e) {
  alert(e.message);
}

map.on("locationfound", onLocationFound);
map.on("locationerror", onLocationError);

map.locate({ setView: true, maxZoom: 16 });
5
  • And what's the problem you're trying to solve, exactly? – IvanSanchez Jan 11 at 23:45
  • hi @IvanSanchez - problem is that if the user denies access the map doesn't load. The dream scenario would be that if that happens, the map still shows at a certain coordinate. – Chris Jenner Jan 11 at 23:59
  • 2
    (1) What code are you usign for initializing the map? Any initial setView? (2) What would be that certain coordinate? – IvanSanchez Jan 12 at 0:09
  • @IvanSanchez var map = L.map("map", { centre: [51.450584 , -2.5946832], zoom: 12, zoomControl: false }); – Chris Jenner Jan 13 at 8:17
  • 3
    Despite my love for british english, the Leaflet option is spelt center and not centre. – IvanSanchez Jan 13 at 12:14
1

You are nearly there with the answer.

The 'Certain Coordinate' value just needs to be a meaningful and useful location. If the map you are building is for a specific City, maybe set it to the CBD.

Or if its a global map, set it to 0,0 at a high map scale (I think Google Earth desktop app does this)

You should do this in your onLocationError function, something like this

function onLocationError(e) {
L.map("map", { center: [51.450584 , -2.5946832], zoom: 12, zoomControl: false });    

}

3
  • Thanks for the response, could give an example of how that would look using the code above? – Chris Jenner Jan 13 at 8:32
  • edited my answer, to include some code. copied your L.map code from a comment above. – nr_aus Jan 14 at 2:47
  • Perfect - it was the spelling of center. All sorted. – Chris Jenner Jan 14 at 13:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.