I am using browser API for geolocation like below (codes written below are just for demo)
Clie开发者_运维问答nt Script using jQuery
$(function () {
$('#link-getlocation').click(function () {
navigator.geolocation.getCurrentPosition(function(loc){
alert(loc.coords.latitude + ', ' + loc.coords.longitude);
});
});
});
Html
<a href="javascript:" id="link-getlocation">Get Location</a>
Demo
http://jsfiddle.net/yhHnD/1/
My question is how browser detects the location coordinates of the user
It depends on the capabilities of the client browser.
- If the device running the browser has GPS (f.ex. a mobile phone), it generally uses GPS based locating
- If the device is on a WLAN, the browser can utilize WLAN location data (eg. via Google Location Services)
- Otherwise it most likely will use a GeoIP database
Most of these depend on OS and browser support and can vary between browsers.
精彩评论