get current latitude and longitude from gps enabled device

☆樱花仙子☆ 提交于 2019-11-30 06:38:34

问题


I would like to get the latitude and longitude of current location from gps enabled mobile device right from the web browser. May I know is this possible? how to do it? does it require geolocation api? Some coding example would be helpful. Thanks.


回答1:


Use the HTML5 Geolocation API, here's the official spec and examples.

EDIT

I've updated my answer to include current browser support.

W3C Geolocation API support

Firefox 3.5+
Safari 5.0+
Chrome 5.0+
Opera
iPhone 3.0+
Android 2.0+

· ·
Other phones not listed above use Gears or their own, platform-specific APIs.

  • W3C geolocation API
  • Gears
  • BlackBerry geolocation API
  • Nokia geolocation API
  • Palm geolocation API
  • OMTP BONDI geolocation API

Ahh, will we ever have just one single API? :)

Many thanks to Mark Pilgrim for his awesome post.




回答2:


Here is an actual JavaScript code which uses HTML5 Geolocation API. The following works on both Android browser and iPhone Safari:

            function onPositionUpdate(position)
            {
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                alert("Current position: " + lat + " " + lng);
            }

            if(navigator.geolocation)
                navigator.geolocation.getCurrentPosition(onPositionUpdate);
            else
                alert("navigator.geolocation is not available");



回答3:


You don’t have to have the newest mobile phone to use GPS and Geolocation API. Almost every mobile browser (without proxy server) can be used to read position from buidin GPS. If You have Java and GPS in Your phone – You can use mobile-gps-web-gate – see at http://code.google.com/p/mobile-gps-web-gate/



来源:https://stackoverflow.com/questions/3452217/get-current-latitude-and-longitude-from-gps-enabled-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!