How does web browser geolocation work? [closed]
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this 开发者_如何学运维questionI would like to know how geolocation works in a web browser like Google Chrome or Firefox?
I can access this web site http://html5demos.com/geo and when I allow the browser to send my location, it shows my current location.
But when I send that URL to my friends to test it, it doesn't work for all of them. How does geolocation work and how do I implement it to get the correct location?
When you visit a location-aware website, Firefox 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. Then Firefox sends this information to the default geolocation service provider, Google Location Services, to get an estimate of your location. That location estimate is then shared with the requesting website.
http://www.mozilla.com/en-US/firefox/geolocation/
So it depends on the browser they are using and some luck :-)
Re how it works: the Wikipedia article discusses several techniques (IP address location, cellphone and WiFi triangulation, GPS).
The HTML5 implementations require both browser support (FF 3.6, Opera 10.60, Chrome 4? 5?, IE maybe some day) and user consent before the geolocation data are retrieved.
As to how to implement it, the code of the demo you link to seems to be under the MIT License which basically says "you can do whatever, as long as you keep the resulting code under the license"; so you could take that code as a base to build on.
Info and examples from W3C Geolocation API Specification:
The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude. The API itself is agnostic of the underlying location information sources. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. No guarantee is given that the API returns the device's actual location.
You could implement it this way:
(one-shot position request)
function showMap(position) {
// Show a map centered at (position.coords.latitude, position.coords.longitude).
}
// One-shot position request.
navigator.geolocation.getCurrentPosition(showMap);
(requesting repeated position updates)
function scrollMap(position) {
// Scrolls the map so that it is centered at (position.coords.latitude, position.coords.longitude).
}
// Request repeated updates.
var watchId = navigator.geolocation.watchPosition(scrollMap);
function buttonClickHandler() {
// Cancel the updates when the user clicks a button.
navigator.geolocation.clearWatch(watchId);
}
Things really depend on the implementation of the website you are using and your browser.
Simplest way, which doesnt require any client side extensions is that the webpage gets your ip address and uses any of the "ip to geolocation" services to make a questimate where you are currently.
Second options is that browsers have an extension that can advertise your your coordinates to the webserver. In these cases, this information in the client side can be either fetched, again from ip to geolocation services or gps unit attached to your computer. For example, Nokia N900 and build-in browser MicroB has this sort of extension.
精彩评论