2014-09-22 129 views
-1

因此,我一直在研究这个主题像疯了一样,每次都得到相同的结果,而且我没有真正得到更多有用的答案。我明白回调函数的概念是什么,但也许我错误地实现了它。所以这就是我所拥有的。从回调函数获取结果并设置为全局变量

我正在研究Google Maps任务,其他所有工作都正常,只是当他们来到页面时,我希望他们的当前位置存储在顶部的'开始'变量中。它可以存储为一个对象或数组,这没有什么区别。我一直在使用一个警告框来测试我的输出,但是我的速度并不快。我会在这里发布我的代码。

//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); 

编辑:为了回应这个问题标记为重复,我会添加一些更多的细节。提供给我的问题的链接对我的帮助并不比我早。它所做的只是重复的异步和同步函数和过程对我来说。我不需要更多。

我正在寻找的是能帮助我打破这个

function updateCoords(callback){ 
    navigator.geolocation.getCurrentPosition(function(position){ 
     var pos = { 
      latitude: position.coords.latitude, 
      longitude: position.coords.longitude 
     }; 
     if() 
     callback(pos); 
    }); 
} 

下来的内饰,以及为什么这个

updateCoords(function(point){ 
    if(point){ 
     start = [ point.latitude, point.longitude]; 
    } 
}); 

完全错过了时机,当它被称为在响应我在这个问题开始时提供的示例代码。我知道'updateCoords'这个函数是异步的,我不需要再重复这些。我阅读了关于与我有关的问题的整个回复,并且再次提供给我的信息不多。所以总结我的问题和我的挫折大部分在这里:

  1. 我在这里进行异步和同步行为。
  2. 我错过了在函数调用期间的特定时间点,我无法精确定位它。
  3. 我尝试了几个不同的例子,包括那些与我相关的问题,结果失败。
  4. 有人可以帮助我确定错误是什么,功能的时间错过,以及为什么会发生,然后帮助我确定适当的解决方案。

回答

0

地理定位功能是异步的。您需要在回调函数中使用结果。

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 

     } 
    }); 
+0

我知道该函数是异步的,但我想要的功能以外的结果。我知道有可能这样做,你甚至读过我打算在做标记之前要做的事情吗? – 2014-09-22 19:11:47

+0

上面的答案不能回答你的问题吗?你可以在函数之外(在启动变量中)获得结果,但很明显,在异步回调返回之前,该值不会被设置,这将是你的代码的计时问题(如果你期望它可用之前)。 – geocodezip 2014-09-22 20:17:45

+0

它没有提供我找的答案没有。我已经意识到你提供的结果会发生,问题在于它迫使我使用updateCoords函数。这是我不能做的事情。我想在页面完成加载时设置启动,而不是回调,并且可以用于全局访问。 – 2014-09-22 20:33:25