I'm trying to use geolocation
to get my current position, which inserts the lat
and lon
into the centre point of a map. I know that geolocation
is asynchronous, so I have to get that value, then call another function for this to pass forward. I am not getting the object mymap created for some reason.
<script>
function foundLocation(pos) {
setMyLocation(pos.coords.latitude, pos.coords.longitude)
};
function noLocation() {
document.getElementById("warning").innerHTML = "Could not find your location";
};
function setMyLocation(myLat, myLon) {
L.marker([ myLat, myLon ], {icon: homeIcon}).bindPopup("You Are Here").addTo(coolPlaces);
var mymap = L.map('mapid', {
center: [ myLat, myLon ],
minZoom: 2,
maxZoom: 18,
zoom: 15,
layers: [mymarkers]
});
};
navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
<snip other JS irrelevant stuff>
</script>
Is there a way I can get those two variables
out of the function
and create the mymap object? I'm not well versed in JS
and its restrictions.
The above code results in no map generated. I have to take mymap outside the geolocation
functions for this to happen. The issue is that I don't know how to send myLat and myLon outside to that declaration.