简体   繁体   中英

How navigator.geolocation.getCurrentPosition gets location coordinates of a client browser

I am using browser API for geolocation like below (codes written below are just for demo)

Client 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM