关于 HTML 5 中的地理位置
Google 地图现在可以在 Firefox 的帮助下精确定位我的街道位置.
Google Maps can now pinpoint my location with street precision with the help of Firefox.
我了解这是兼容 HTML 5 的浏览器的一项新功能,并且该位置是通过使用连接的 WiFi 网络的某种功能获取的(我希望我没有做出任何愚蠢的假设).
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:
- 为什么只在 HTML 5 中?
- Firefox 为什么/如何要求我与 Google 地图分享我的位置?
- 可以指望的正常精度是多少?
- 如何在我的网站中实现此功能?
提前致谢!
推荐答案
它是如何工作的?
当您在 Firefox 中访问位置感知网站时,浏览器会询问您是否要共享您的位置.
When you visit a location-aware website in Firefox, the browser will ask you if you want to share your location.
如果您同意,Firefox 会收集有关附近无线接入点和您计算机的 IP 地址的信息,并将通过将此信息发送到 Google 位置服务(Firefox 中的默认地理位置服务).然后与请求的网站共享该位置估计.(来源)
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)
位置的准确性如何?
准确性因地点而异.在某些地方,地理定位服务提供商可能能够提供几米以内的位置.然而,在其他领域,它可能远不止于此.由于无法保证所提供位置的准确性,所有位置均被视为估计值.(来源)
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)
就我而言,Firefox 报告说我离我的真实位置大约 10 公里.
In my case, Firefox reports that I am about 10km away from my real location.
如何在我的网站中使用此功能?
你会这样做:
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.)
You can see an online demo here: Firefox HTML 5 Geolocation Demo (Requires a geolocation-aware browser such as Firefox 3.1b3.)
相关文章