1

携帯電話またはタブレットに GPS が搭載されている場合、 Web サイトの訪問者の正確な位置を取得する必要があります... GPS を使用して訪問者の正確な緯度と経度を取得する必要があります...

さらに検索しましたが、結果はすべてモバイル アプリに表示されますが、ブラウザ ベースの Web サイトに実装する必要があります

それは可能ですか?..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 に答える