0

私はしばらくこれに取り組んできましたが、一日中問題を抱えていました。私は今、本当に立ち往生していて、次にどこに行くべきかわからないところにいます. 私はJavascriptにかなり慣れていないので、おそらくnoobエラーであると指摘します。

とにかく、全体的な目的は、ユーザーの現在の位置を確認し、ターゲット座標の半径 200 m 内にいるかどうかを確認することです。現在の座標を入力すると、OnInterval 関数でアラートが実行されますが、半径 200m にある場合は実行されません。

これを修正する方法を知っている人はいますか?:バツ

var userPositionLat;
var userPositionLong;

jQuery(window).ready(function() {  
    initiate_geolocation();
});

function initiate_geolocation() {  
    //navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
    // http://www.thedotproduct.org/2010/04/example-of-navigator-geolocation-watchposition/
    navigator.geolocation.watchPosition(handle_geolocation_query,handle_errors, {enableHighAccuracy:true, maximumAge:30000});
}

function handle_errors(error) {  
    switch(error.code)  
    {  
        case error.PERMISSION_DENIED: alert("user did not share geolocation data");  
        break;

        case error.POSITION_UNAVAILABLE: alert("could not detect current position");  
        break;

        case error.TIMEOUT: alert("retrieving position timed out");  
        break;

        default: alert("unknown error");  
        break;
    }  
}  



function handle_geolocation_query(position) {
    userPositionLat = position.coords.latitude;
    userPositionLong = position.coords.longitude;

    alert('Your latitude is :'+userPositionLat+' and longitude is '+userPositionLong);

    IsUserNearTarget();
}


function CalculateDistance(lat1, long1, lat2, long2) {
    // Translate to a distance
    var distance =
            Math.sin(lat1 * Math.PI) * Math.sin(lat2 * Math.PI) +
            Math.cos(lat1 * Math.PI) * Math.cos(lat2 * Math.PI) * Math.cos(Math.abs(long1 - long2) * Math.PI);

    // Return the distance in meters
    return Math.acos(distance) * 6370981.162;
}

function IsUserNearTarget()
{
    // The target longitude and latitude
    var targetlong = 0.0; //change this with your coordinates to test
    var targetlat = 0.0; //change this with your coordinates to test

    // Start an interval every 1s
    var OurInterval = setInterval(OnInterval, 1000);

    // Call this on an interval
    function OnInterval() {
        var distance = CalculateDistance(targetlat, targetlong, userPositionLat, userPositionLong);

        // Is it in the right distance? (200m)
        if (distance <= 200) {
            // Stop the interval
            clearInterval(OurInterval);

            alert('TOTALLY WORKS BRO');
        }
    }
}
4

1 に答える 1

1

度をラジアンに変換するにはπ/180、 ではなくを掛けπます。

于 2013-03-02T16:09:20.723 に答える