关于 HTML 5 中的地理定位

发布于 2024-08-20 18:47:10 字数 301 浏览 4 评论 0原文

现在,Google 地图可以在 Firefox 的帮助下以街道精度精确定位我的位置。

我知道这是 HTML 5 兼容浏览器的一项新功能,并且该位置是通过使用连接的 WiFi 网络的某种功能来获取的(我希望我没有做出任何愚蠢的假设)。

我想知道的是整个过程是如何运作的:

  • 为什么只在 HTML 5 中?
  • Firefox 为什么/如何要求我与 Google 地图共享我的位置?
  • 可以信赖的正常精度是多少?
  • 如何在我的网站中实现此功能?

提前致谢!

Google Maps can now pinpoint my location with street precision with the help of Firefox.

I understand this is a new feature of HTML 5 compatible browsers and that the location is fetched by using some sort of feature of the connected WiFi network (I hope I'm not making any silly assumptions).

What I intend to know is how this whole process exactly works:

  • Why only in HTML 5?
  • Why / how does Firefox ask me to share my location with Google Maps?
  • What is the normal precision one can count on?
  • How can I implement this feature in my websites?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜吻♂芭芘 2024-08-27 18:47:10

它是如何工作的?

当您在 Firefox 中访问位置感知网站时,浏览器会询问您是否要共享您的位置。

如果您同意,Firefox 会收集有关附近无线接入点和您计算机的 IP 地址的信息,并通过将此信息发送到 Google 定位服务(Firefox 中的默认地理定位服务)。然后,该位置估计会与请求网站共享。 (来源

位置的准确度如何?

不同地点的准确度差异很大。在某些地方,地理定位服务提供商可能能够提供几米以内的位置。然而,在其他领域,情况可能远不止于此。所有位置均应视为估计值,因为无法保证所提供位置的准确性。 (来源)

就我而言,Firefox 报告我距离大约 10 公里我的真实位置。

如何在我的网站中使用此功能?

您可以执行以下操作:

if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) {  

        alert(position.coords.latitude + ", " + position.coords.longitude);

        // Use the latitude and location as you wish. You may set a marker
        // on Google Maps, for example.
    }); 
} 
else {
    alert("Geolocation services are not supported by your browser.");
}  

您可以在此处查看在线演示:Firefox HTML 5 地理位置演示(需要支持地理位置的浏览器,例如 Firefox 3.1b3。)

How does it work?

When you visit a location-aware website in Firefox, the browser will ask you if you want to share your location.

If you consent, Firefox gathers information about nearby wireless access points and your computer’s IP address, and will get an estimate of your location by sending this information to Google Location Services (the default geolocation service in Firefox). That location estimate is then shared with the requesting website. (Source)

How accurate are the locations?

Accuracy varies greatly from location to location. In some places, the geolocation service providers may be able to provide a location to within a few meters. However, in other areas it might be much more than that. All locations are to be considered estimates as there is no guarantee on the accuracy of the locations provided. (Source)

In my case, Firefox reports that I am about 10km away from my real location.

How do I use this feature in my website?

You would do something like this:

if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) {  

        alert(position.coords.latitude + ", " + position.coords.longitude);

        // Use the latitude and location as you wish. You may set a marker
        // on Google Maps, for example.
    }); 
} 
else {
    alert("Geolocation services are not supported by your browser.");
}  

You can see an online demo here: Firefox HTML 5 Geolocation Demo (Requires a geolocation-aware browser such as Firefox 3.1b3.)

万劫不复 2024-08-27 18:47:10

HTML5 提供API< /a>

which allows the web browser (and then hence the server-side of an web application) to query the location and related information such as speed (if relevant), in a standard, uniform, fashion.

主机及其 Web 浏览器提供计算/估计地理位置本身的“设备”。

要使此 API 有用,需要底层主机和 Web 浏览器
  a) 允许共享此类信息(注意隐私问题)以及
  b) 具备一定的装备(本地或通过其所连接的网络)来读取或估计地理位置。

计算实际位置所涉及的技术和设备涉及以下各项的组合(当然并非全部适用),并且独立于 HTML 5 标准

  • GPS 设备(现在很多手机都配备了它们)
  • 手机网络级别的路由信息
  • ​​ IP 地址/ISP 路由信息
  • Wifi 路由器信息
  • 固定数据,手动输入(对于位于固定位置的电脑)
  • ...

因此...
- HTML5 单独无法确定地理定位:升级到较新的网络浏览器本身不足以在您的应用程序等中获得地理定位功能。
- 地理位置数据可以在 HTML5 API 之外共享,从而允许 GPS 就绪或 GeoLocation 就绪的手机在其他 API 中公开地理位置数据。

HTML5 supplies an API

which allows the web browser (and then hence the server-side of an web application) to query the location and related information such as speed (if relevant), in a standard, uniform, fashion.

The host and its web browser supply the "devices" which compute/estimate the geolocation per-se

For this API to be useful, requires that the underlying host and web browser
  a) allow the sharing of such info (note the privacy issue) and
  b) be somewhat equipped (either locally or by way of the network they are hooked-up to) to read or estimate the geolocation.

The techniques and devices involved in computing the actual location involves a combination of the following (not all apply of course), and is independent from the HTML 5 standard:

  • GPS device (lots of phones now have them)
  • Routing info at the level of the Cell phone network
  • IP address / ISP routing information
  • Wifi router info
  • Fixed data, manually input (for pcs which are at a fixed location)
  • ...

Therefore...
- HTML5 alone cannot figure out geolocation: upgrading to newer web browser, in of itself, won't be sufficient to get geolocation features in your applications etc.
- Geolocation data can be shared outside of the HTML5 API, allowing GPS-ready or GeoLocation-ready phones expose the geolocation data within other APIs.

尹雨沫 2024-08-27 18:47:10

HTML5 Geolocation API 使用用户设备中的某些功能,例如全球定位系统 (GPS)、设备的互联网协议 (IP) 地址、最近的移动电话塔以及用户的输入,检索用户的位置。使用 Geolocation API 检索的用户位置几乎是准确的,具体取决于用于检索位置的源类型。

这里有一个非常好的 HTML5 地理定位演示 (http://html5demos.com/geo)。每当网站尝试使用以下提到的 API 之一获取您的位置时,浏览器都会在调用 API 共享您的位置之前询问您的许可。

Geolocation API 提供以下方法:

  • getCurrentPosition(successCallback, errorCallback, options)

    用于检索用户当前的地理位置。

  • watchPosition(successCallback、errorCallback、选项)

    该函数返回一个 watchId 并使用更新后的坐标调用 successCallback。当用户移动时,它会继续返回更新的位置(就像汽车中的 GPS)。

  • clearWatch(watchId)

    根据提供的 watchId 停止 watchPosition() 方法。

示例代码:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(userPositionSuccess, userPositionError);
} else {
  alert("Your browser does not support geolocation.");
}

function userPositionSuccess(position) {
  alert("Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude);
}

function userPositionError() {
  alert("There was an error retrieving your location!");
}

HTML5 Geolocation API uses certain features, such as Global Positioning System (GPS), Internet Protocol (IP) address of a device, nearest mobile phone towers, and input from a user, in the users’ device to retrieve the users’ location. The users’ location retrieved by using the Geolocation API is almost accurate depending upon the type of source used to retrieve the location.

There is a really good demo of HTML5 Geolocation here (http://html5demos.com/geo). Whenever a website tries to fetch your location by using one of the following mentioned APIs, the browser will ask me your permission before invoking the API to share your location.

The Geolocation API provides the following methods:

  • getCurrentPosition(successCallback, errorCallback, options)

    Used to retrieve the current geographical location of a user.

  • watchPosition(successCallback, errorCallback, options)

    The function returns a watchId and calls successCallback with the updated coordinates. It continues to return updated position as the user moves (like the GPS in a car).

  • clearWatch(watchId)

    Stops the watchPosition() method based on the watchId provided.

Sample Code:

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(userPositionSuccess, userPositionError);
} else {
  alert("Your browser does not support geolocation.");
}

function userPositionSuccess(position) {
  alert("Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude);
}

function userPositionError() {
  alert("There was an error retrieving your location!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文