【问题标题】:Does html5 gelocation method position.address.city only work in Firefoxhtml5 地理定位方法 position.address.city 是否仅在 Firefox 中有效
【发布时间】:2010-08-25 15:52:55
【问题描述】:

city 方法是否仅适用于 Firefox?例如,如果我想打印用户所在的城市:

  if (navigator.geolocation)
  {
    navigator.geolocation.getCurrentPosition(function(location) {
      document.write(location.address.city);
    });
  }

如果这是真的,那么找到用户城市的最佳方法是什么?获取经纬度并将其插入谷歌的位置 api?

【问题讨论】:

  • address 属性绝对不是当前 W3C Geolocation API 规范的一部分(尽管第二个版本正在讨论城市地址),所以我怀疑其他浏览器不支持它。 (不过我没有亲自检查过;我也没有意识到 FF 也有这个功能。)

标签: javascript firefox html geolocation


【解决方案1】:

是的,location.address.city 仅适用于 Firefox。我刚刚确认 Safari 或 Chrome 都不支持 address 属性 - 两者都返回“未定义”。

您建议通过将 lat/long 插入 Google's Geocoding API 来检索用户的城市应该是一个可行的解决方案。

【讨论】:

    【解决方案2】:

    您可以使用此代码:

    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    var geocoder = new google.maps.Geocoder()
    geocoder.geocode({location:latlng},function(result){
                        document.write(resul.address_components.long_name)
    });
    

    更多信息请查看the reference at google code

    【讨论】:

      【解决方案3】:

      不,它适用于all major browsers

      here 为例,它似乎确实符合您的建议

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

      另外,一个简单的谷歌搜索会在大约 15 秒内为您提供相同的信息 :)

      【讨论】:

      • 我专门询问 location.address.city 方法。它可以在 nsIDOMGeoPositionAddress 下的 mozilla 文档中找到。地址接口是不是只有firefox才有?
      猜你喜欢
      • 1970-01-01
      • 2012-08-04
      • 2011-01-11
      • 2011-03-30
      • 2012-05-26
      • 1970-01-01
      • 2013-10-13
      • 2012-03-26
      • 2015-11-28
      相关资源
      最近更新 更多