最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I read the 'real' mobile GPS coordinates from a website? - Stack Overflow

programmeradmin1浏览0评论

I did a website and I want to read the 'real' position GPS on mobile (Android & iPhone). When I try set the location on my website from my Android with W3C javascript method the GPS is not enabled and the position is set by IP (When I try with Google Maps app the GPS is enabled and blink on the status bar). Is any way for read the GPS (real GPS) from a web on a mobile? Thanks in advance!

I did a website and I want to read the 'real' position GPS on mobile (Android & iPhone). When I try set the location on my website from my Android with W3C javascript method the GPS is not enabled and the position is set by IP (When I try with Google Maps app the GPS is enabled and blink on the status bar). Is any way for read the GPS (real GPS) from a web on a mobile? Thanks in advance!

Share Improve this question edited Apr 16, 2012 at 20:06 gakhov 1,9714 gold badges28 silver badges39 bronze badges asked Apr 16, 2012 at 18:12 user512663user512663 1731 silver badge10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

With HTML5 you can do that. You need to check Geolocation API: Dive Into HTML5 and W3C Geolocation API Specification

The simplest example looks like:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition( 
        function (position) {  
            do_something(position.coords.latitude,position.coords.longitude);
        }, 
        function (error){
            switch(error.code){
                case error.TIMEOUT:
                    // Timeout
                    break;
                case error.POSITION_UNAVAILABLE:
                    // Position unavailable
                    break;
                case error.PERMISSION_DENIED:
                    // Permission denied
                    break;
                case error.UNKNOWN_ERROR:
                    // Unknown error
                    break;
                default: break;
           }
        }
    );
}

Probably you're also interesting in some browser specific implementations: Mozilla, IE, Chrome

Updated. As Mozilla said here, Devices with a GPS, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or wifi) may be returned to getCurrentPosition() to start.

So, if you need to have high accuracy (like GPS only), use watchPosition instead of getCurrentPosition.

As stated in the W3C specification, you might need to use the third argument of both watchPosition and getCurrentPosition, which is a PositionOptions interface to set enableHighAccuracy to true, else, using its default value false, the GPS won't necessarily be switched on by the device.

发布评论

评论列表(0)

  1. 暂无评论