【问题标题】:GeoLocation HTML5 getCurrentLocation callbackGeoLocation HTML5 getCurrentLocation 回调
【发布时间】:2011-07-16 15:38:25
【问题描述】:

如何为 getCurrentLocation 方法添加回调函数,将坐标或位置作为参数传递给全局变量?该对象允许将坐标传递给 getCurrentLocation 之外的全局变量。

//Global scope
var Location= {coords:null;
} 

function getLocation(position)
{
  position.coords.latitude;
  Location.coords=coords.clone();
}

somewhere
navigator.geolocation.getCurrentLocation(getLocation, error)
Location.coords is NULL !

谢谢。

【问题讨论】:

    标签: html geolocation


    【解决方案1】:

    不应该:

    getCurrentPosition
    

    【讨论】:

      【解决方案2】:
      navigator.geolocation.getCurrentLocation(function(pos){
        console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
      });
      

      建议也使用 Paul Irish 的 shim 作为旧浏览器的后备:https://gist.github.com/366184

      【讨论】:

        【解决方案3】:

        【讨论】:

          【解决方案4】:

          在您的代码中这一行:

          Location.coords=coords.clone();
          

          使用未声明的变量coords。具有您调用的坐标的函数的参数position。是不是笔误?

          【讨论】:

            【解决方案5】:

            您可以在http://diveintohtml5.ep.io/geolocation.html 阅读有关如何使用 geoloction API 的所有信息,这是一个很棒的网站。默认情况下,getCurrentLocation 将回调作为参数。回调接收一个位置对象。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-02-13
              • 2011-09-10
              • 2012-08-14
              • 1970-01-01
              • 2013-07-22
              • 2013-04-15
              • 2013-01-26
              相关资源
              最近更新 更多