I have a json object which is giving me the users latitude and longitude coordinates. I want to use these coordinates to show the user their location on google maps.
I'm saving these values in two variables and plugging them into the google maps api script but the map doesn't show up. It does take the variables a tiny bit of time to show up but I tried wrapping the google maps script in a setTimeout function with no luck.
Is there any way to do this without having to make the user click a button to load the map manually?
$(document).ready(function() {
var userLat;
var userLng;
$.getJSON("http://www.sample.com/callback=?",
function(json) {
console.log(json);
userLat = (json.latitude);
userLng = (json.longitude);
}
)
})
function initialize() {
var mapOptions = {
center: { lat: userLat, lng: userLng},
zoom: 8,
scrollwheel: false
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
userLatanduserLngto generate the new map or update the old one?initializefunction from within your$.getJSONfunction after you define the latitude and longitude you require. When you callinitializeonwindow.load, theJSONcallback might not have executed and so you couldn't see the map getting updated