Haider
Haider

Reputation: 615

HTML5 Geolocation on distance callback

Is there any function available in HTML5 Geolocation to get the location callback on distance traveled e.g. on every 1000 meter i want a lat lng.

i can use

navigator.geolocation.watchPosition(success, error, options); 

and on the success callback calculate the distance between this and previous point if its greater than 1000 fire my distance callback, is there any other solution?

Upvotes: 1

Views: 244

Answers (1)

Prabhu Vignesh Rajagopal
Prabhu Vignesh Rajagopal

Reputation: 1078

Use this function to get lat and long:

function initiate_watchlocation() {
    if (watchProcess == null) {
        watchProcess = navigator.geolocation.watchPosition(onSuccess, onError);
    }
}
var onSuccess = function(position) {
    var lat= position.coords.latitude;
    var long= position.coords.longitude;

    };

    function onError(error) {
            alert_box('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

and keep it in DB and map that to lat1 and lon1, then keep watching lat and lon and map it to lat2 and lon2 respectively. and keep performing below function with old one.

function distance(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var radlon1 = Math.PI * lon1/180
    var radlon2 = Math.PI * lon2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist
}

then perform this:

if(distance(lat1, lon1, lat2, lon2, K) ==1){
   alert("1000 m");
}

Upvotes: 1

Related Questions