1

如果他们的手机或平板电脑中有 GPS,我需要在我的网站上获得访问者的准确位置...我需要使用他们的 GPS 获得访问者的准确纬度和经度...

我搜索了更多,但所有结果都显示了移动应用程序,但我需要在基于浏览器的网站中实现

可能吗?..有任何API吗?

4

1 回答 1

3

您可以使用W3C HTML5 Geolocation API。这不需要移动应用程序,并且所有主要移动浏览器都支持该 API。你可以用它来做这样的事情:

MIN_ACCEPTABLE_ACCURACY = 20; // Minimum accuracy in metres that is acceptable as an "accurate" position

if(!navigator.geolocation){
    console.warn("Geolocation not supported by the browser");
    return;
}

navigator.geolocation.watchPosition(function(position){

    if(position.accuracy > MIN_ACCEPTABLE_ACCURACY){
        console.warn("Position is too inaccurate; accuracy="+position.accuracy");
        return;
    }else{
        // Do something with the position

        // This is the current position of your user
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    }

}, function(error){
    switch(error.code) {
        case error.PERMISSION_DENIED:
            console.error("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            console.error("Location information is unavailable.");
            break;
        case error.TIMEOUT:
            console.error("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
            console.error("An unknown error occurred.");
            break;
    }
},{
    timeout: 30000, // Report error if no position update within 30 seconds
    maximumAge: 30000, // Use a cached position up to 30 seconds old
    enableHighAccuracy: true // Enabling high accuracy tells it to use GPS if it's available  
});
于 2013-07-16T15:46:39.310 回答