【问题标题】:Google javascript maps getCurrentPosition谷歌 javascript 地图 getCurrentPosition
【发布时间】:2015-01-12 20:06:43
【问题描述】:

我该如何使用:

navigator.geolocation.getCurrentPosition()

获取当前位置的坐标。
这是来自谷歌网站的例子:

function initialize() {
 var mapOptions = {
   zoom: 6
 };
 map = new google.maps.Map(document.getElementById('map-canvas'),
     mapOptions);

 // Try HTML5 geolocation
 if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
       var pos = new google.maps.LatLng(position.coords.latitude,
                                   position.coords.longitude);

       var infowindow = new google.maps.InfoWindow({
            map: map,
            position: pos,
            content: 'Location found using HTML5.'
            });
        map.setCenter(pos);
      }, function() {
           handleNoGeolocation(true);
         });
 } else {
   // Browser doesn't support Geolocation
      handleNoGeolocation(false);
 }
}

我尝试将 var pos 部分替换为全局变量 myPos ,但没有成功。
我的意思是我总是在初始化()函数之后得到 myPos 未定义。
在加载表单(窗口)时调用的初始化函数中获取纬度和经度 navigator.geolocation.getCurrentPosition() 的正确方法是什么?

【问题讨论】:

  • 地理定位服务是异步的(所以在回调函数运行之前不会定义位置)。 Your code as posted works for me
  • 也许你是对的,除非调用 getcurrentposition,否则我怎么能告诉函数不继续?
  • 您要求采用异步函数,由于某种原因,它是异步的,并使其同步...研究/了解异步函数。

标签: javascript google-maps


【解决方案1】:

.getCurrentPosition() 是一个异步函数,因此它需要一个回调,一旦它具有这些坐标就会执行,例如

navigator.geolocation.getCurrentPosition(function(position){
  console.log(position);
});

这会给你这样的东西:

{
  "timestamp": 1421093714138,
  "coords":
    {
      "speed": null,
      "heading": null,
      "altitudeAccuracy": null,
      "accuracy": 20,
      "altitude": null,
      "longitude": -122.4091036,
      "latitude": 37.7837543
    }
}

在您传递.getCurrentPosition 的回调中,您甚至可以更新变量,假设它们是事先声明的。我猜你的 myPos 变量未定义的原因是因为你连接到谷歌地图 API 的方式有问题。如果您不使用谷歌地图,而只想获取位置,则可以执行以下操作:

var myPos;

navigator.geolocation.getCurrentPosition(function(position){
  myPos = position;
});

哦,请确保您允许该网站访问您的位置。在 chrome 中,您会在页面顶部看到一个栏,上面写着“ 想要使用您计算机的位置 [Deny] [Allow]”

编辑:

有两个问题。您只能在回调函数的范围内访问该变量——只有在该函数运行后,tmpPos 才会被定义。正如我上面所说,.getCurrentPosition 是一个asynchronous 函数。这意味着它会设置一个流程来获取您的地理位置,但同时会执行其他操作(在您的情况下,它会继续前进并尝试将其他变量更新为它还没有的信息)。

此外,您在其自身内部调用初始化函数,这样将创建一个永不结束的无限循环函数。要解决此问题,请尝试:

function initialize(){
navigator.geolocation.getCurrentPosition(function(position){
  // create the map here, because we only have access to position inside of this function
  // even if we store in a global variable, it only gets updated once this callback runs

  var currentPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
}

initialize();

【讨论】:

  • 我现在在 myPos 中得到一个 Geoposition 对象,但是如果我尝试使用 myPos.coords.latitude 访问例如纬度,我得到异常:“未捕获的类型错误:无法读取未定义的属性“坐标” " ,虽然如果我打开变量有坐标部分,我可以看到纬度和经度的数字。见 [i61.tinypic.com/21l72o6.png]
  • 有一些问题。更新了我的答案。查看这个关于回调的资源(javascriptissexy.com/understand-javascript-callback-functions-and-use-them/),我认为这将有助于您对异步 JS 编码模式的一般理解
猜你喜欢
  • 2017-01-03
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多