【问题标题】:Get GPS location from the web browser从网络浏览器获取 GPS 位置
【发布时间】:2011-02-04 08:37:43
【问题描述】:

我正在开发一个基于移动的网站,我已经集成了谷歌地图,我需要动态填写谷歌地图的“发件人”字段。

是否可以从网络浏览器获取 GPS 位置并动态填写在 Google 地图的“发件人”字段中?

【问题讨论】:

  • 您打算支持哪些手机?
  • 我想支持所有的手机浏览器,但i-phone是最优先的
  • 您最好编写一个原生应用程序,因为它可以更紧密地与可用的硬件集成
  • 如果只需要位置信息,那么原生应用程序就太过分了。 iOS 不是唯一的移动设备,它只是一个优先事项。

标签: javascript mobile geolocation


【解决方案1】:

如果你使用Geolocation API,就跟使用下面的代码一样简单。

navigator.geolocation.getCurrentPosition(function(location) {
  console.log(location.coords.latitude);
  console.log(location.coords.longitude);
  console.log(location.coords.accuracy);
});

您也可以使用Google's Client Location API

Is it possible to detect a mobile browser's GPS location?Get position data from mobile browser 已讨论过此问题。您可以在那里找到更多信息。

【讨论】:

【解决方案2】:

给出更具体的答案。 HTML5 允许您获取地理坐标,并且做得相当不错。总体而言,浏览器对地理定位的支持非常好,除了 ie7 和 ie8(以及opera mini)之外的所有主流浏览器。 IE9 完成了这项工作,但性能最差。结帐 caniuse.com:

http://caniuse.com/#search=geol

此外,您还需要获得用户的批准才能访问他们的位置,因此请务必检查这一点并提供一些体面的说明,以防它被关闭。尤其是 iPhone 为 Safari 开启权限有点麻烦。

【讨论】:

【解决方案3】:

使用这个,你会在http://www.w3schools.com/html/html5_geolocation.asp找到所有信息

<script>
var x = document.getElementById("demo");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
}
</script>

【讨论】:

  • 请注意:getCurrentPosition() 和 watchPosition() 不再适用于不安全的来源。要使用此功能,您应该考虑将应用程序切换到安全源,例如 HTTPS。
【解决方案4】:

GeoLocation API,但目前浏览器支持相当薄弱。大多数关心此类事情的网站都使用 GeoIP 数据库(通常附带关于此类系统不准确的附带条件)。您还可以查看需要用户合作的第三方服务,例如FireEagle

【讨论】:

  • 非常感谢您的快速回复。如果可能的话,你能不能再给我一些提示...
【解决方案5】:

可观察

/*
  function geo_success(position) {
    do_something(position.coords.latitude, position.coords.longitude);
  }

  function geo_error() {
    alert("Sorry, no position available.");
  }

  var geo_options = {
    enableHighAccuracy: true,
    maximumAge        : 30000,
    timeout           : 27000
  };

  var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
  */
  getLocation(): Observable<Position> {
    return Observable.create((observer) => {
      const watchID = navigator.geolocation.watchPosition((position: Position) => {
        observer.next(position);
      });
      return () => {
        navigator.geolocation.clearWatch(watchID);
      };
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }

【讨论】:

    【解决方案6】:

    让我们使用最新的fat arrow functions

    navigator.geolocation.getCurrentPosition((loc) => {
      console.log('The location in lat lon format is: [', loc.coords.latitude, ',', loc.coords.longitude, ']');
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-23
      • 2014-04-16
      • 2013-12-11
      • 2010-10-23
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多