I'm trying to center my current location in Google Maps. This will center a location with a specific latitude- and longitude-variable.
var coords = new google.maps.LatLng(62.39081100, 17.30692700);
But I tried to grab the users location with this function:
function grabMyPosition() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(centerMe);
} else {
alert("You don't support this");
}
}
function centerMe(center) {
var me_location = {
lat: position.coords.latitude,
lon: position.coords.longitude
};
}
And then do this:
var coords = new google.maps.LatLng(me_location.lat, me_location.lon);
But then I get this:
Uncaught TypeError: Cannot read property 'lat' of undefined
That's because I'm storing and filling this "me_location" variable inside a function. How can I make it "global" so it could be used outside my function?
me_location
is not accessible from outsideCenterMe
function..navigator.geolocation.getCurrentPosition(centerMe());
instead.navigator.geolocation.getCurrentPosition
receive a callback function as argument.. so center the map inside callback..