So I've been researching this topic like crazy, and I get the same results each time, and I'm not really getting anymore answers that are becoming helpful. I understand what the idea of a callback function is, but perhaps I am implementing it wrong. So here's what I've got.
I'm working on a Google Maps task, I have everything else working correctly, it's just when they come to the page I would like their current location stored away in the 'start' variable at the top. It can be stored as an Object or an array, that doesn't make a difference.I'v been using an alert box to test my output, but I'm getting nowhere fast. I'll post my code below here.
//set global variables that will be used for the maps later on in the script
var pj;
var tifton;
var nlv;
var direction;
var start ='default';
function initialize(){
pj = new google.maps.LatLng(41.048899, -95.822685); //map for pacific junction
tifton = new google.maps.LatLng(31.417032, -83.498785); //map for tifton
nlv = new google.maps.LatLng(36.228928, -115.112426); //map for north las vegas
//set the directions for the user
updateCoords(function(point){
if(point){
start = point.latitude;
}
});
alert(start); //it will not stop showing default as the result
drawMap(pj,"pj-map", "Pacific Junction, IA");
drawMap(tifton, "tifton-map", "Tifton, GA");
drawMap(nlv, "nlv-map", "North Las Vegas, NV")
}
function drawMap(coords, div, title){
var opts = {
zoom: 8,
center: coords,
panControl: false,
zoomControl: false,
scaleControl: false,
scrollwheel: false,
draggable: false,
disableDoubleClickZoom: true
}; //generate map options for each individual map
var map = new google.maps.Map(document.getElementById(div), opts); //set the canvas that the map will be drawn to
var marker = new google.maps.Marker({
position: coords,
title: title
});
marker.setMap(map);
}
function updateCoords(callback){
navigator.geolocation.getCurrentPosition(function(position){
var pos = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
callback(pos);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
EDIT: In response to having this question marked as a duplication, I will add some more detail. The link to the question that was provided to me, did not assist me any further than I already was. All it did was re iterate was asynchronous and synchronous functions and processes were to me. I do not need any more of that.
What I'm looking for is a response that can help me break down the interior of this
function updateCoords(callback){
navigator.geolocation.getCurrentPosition(function(position){
var pos = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
if()
callback(pos);
});
}
and why this
updateCoords(function(point){
if(point){
start = [ point.latitude, point.longitude];
}
});
is completely missing the timing when it is called in the example code that I provided at the very start of this question. I know that the function 'updateCoords' is asynchronous, I do not need that repeated to me further. I read that entire response on the question that was linked to me, and again it provided me no new information. So to sum up my question and the bulk of my frustrations here:
- I have asynchronous and sychronous behaviors going on here.
- I am missing the timing at a specific point during the call of the function, I can not pinpoint it.
- I have tried several different examples that I have found including those that were in the question linked to me, results failed.
- Can someone help me identify what the errors is, where the timing of the functions is being missed, and why that occurs, and then assist me in indentifying a proper solution to that issue.