Distance between two gps coodinates - javascript -
i searching know concept behind since last month , till got these resources
- how calculate distance between 2 latitude-longitude points?
- geolib.js
- function calculate distance between 2 coordinates shows wrong
- calculate distance, bearing , more between latitude/longitude points
but still can't figure out right distance between specified coordinates
as instance, have 2 coordinates
start lat: 26.26594 start long: 78.2095508 destination lat: 21.24386 destination long: 81.611
and got result 655.358 km, (i had measured using bike's odometer) ~3 km far, why getting answer.
i had checked implementation on geolib.js (2nd item have specified in list items), showing same result (655.358).
i can't figure out
js
// inititalising distance calculation function geolocationinit() { $('.fd-loc-err').html(''); // empty error shown, if there $('.fd-dist-rest-user').html('<img src="/images/loader.gif" alt="loading"/>'); // loader var options = {timeout:120000}; if (navigator.geolocation) { navigator.geolocation.watchposition(getlocation, goterror, options); } else { locationerror = 'your browser not support geolocation'; showlocationerror(locationerror); } } //finding coordinates of user function getlocation(current) { var userlat = current.coords.latitude, userlong = current.coords.longitude; var distance = getdistance(userlat, userlong, restlatitude, restlongitude); $('.fd-dist-rest-user').html('~' + (distance/1000).tofixed(2) + ' km away'); // fd-dist-rest-user <div> tag have show distance calculated } function torad(value) { return value * math.pi / 180; } // calculating distance using vincenty formula function getdistance(lat1, lon1, lat2, lon2) { var = 6378137, b = 6356752.314245, f = 1/298.257223563; var l = torad(lon2-lon1); var u1 = math.atan((1-f) * math.tan(torad(lat1))); var u2 = math.atan((1-f) * math.tan(torad(lat2))); var sinu1 = math.sin(u1), cosu1 = math.cos(u1); var sinu2 = math.sin(u2), cosu2 = math.cos(u2); var lambda = l, lambdap, iterlimit = 100; { var sinlambda = math.sin(lambda), coslambda = math.cos(lambda); var sinsigma = math.sqrt((cosu2*sinlambda) * (cosu2*sinlambda) + (cosu1*sinu2-sinu1*cosu2*coslambda) * (cosu1*sinu2-sinu1*cosu2*coslambda)); if (sinsigma==0) return 0; var cossigma = sinu1*sinu2 + cosu1*cosu2*coslambda; var sigma = math.atan2(sinsigma, cossigma); var sinalpha = cosu1 * cosu2 * sinlambda / sinsigma; var cossqalpha = 1 - sinalpha*sinalpha; var cos2sigmam = cossigma - 2*sinu1*sinu2/cossqalpha; if (isnan(cos2sigmam)) cos2sigmam = 0; var c = f/16*cossqalpha*(4+f*(4-3*cossqalpha)); lambdap = lambda; lambda = l + (1-c) * f * sinalpha * (sigma + c*sinsigma*(cos2sigmam+c*cossigma*(-1+2*cos2sigmam*cos2sigmam))); } while (math.abs(lambda-lambdap) > 1e-12 && --iterlimit>0); if (iterlimit==0) return nan var usq = cossqalpha * (a*a - b*b) / (b*b); var = 1 + usq/16384*(4096+usq*(-768+usq*(320-175*usq))); var b = usq/1024 * (256+usq*(-128+usq*(74-47*usq))); var deltasigma = b*sinsigma*(cos2sigmam+b/4*(cossigma*(-1+2*cos2sigmam*cos2sigmam)-b/6*cos2sigmam*(-3+4*sinsigma*sinsigma)*(-3+4*cos2sigmam*cos2sigmam))); var s = b*a*(sigma-deltasigma); return s; } // error handling geolocation function goterror(error) { switch(error.code) { case 1: locationerror = 'you need give permission use location calculate distances between restaurant , <button class="btn btn-danger fd-detect-lctn-try-agn">please try again</button>'; break; case 2: locationerror = 'time out - need give permission use location show distance of restaurant current position <button class="btn btn-danger fd-detect-lctn-try-agn">please try again</button>'; break; case 3: locationerror = 'it took long getting position, <button class="btn btn-danger fd-detect-lctn">please try again</button>'; break; default: locationerror = 'an unknown error has occurred, <button class="btn btn-danger fd-detect-lctn">please try again</button>'; } showlocationerror(locationerror); } function showlocationerror(msg) { $('.fd-loc-err').html(msg); $('.fd-dist-rest-user').html(''); }
i getting latitude , longitude database calling geolocationinit function on button click (below following code)
restlongitude = response['longitude']; restlatitude = response['latitude']; geolocationinit(); /* getting location initialisation */
thanks in advance solve probelm
after more research of comments patrick evans, have found that, getting problem due lack of gps device on desktops , because of that, coordinates not getting accurate leads wrong calculation of distances.
according firefox - location aware browsing
accuracy varies location location. in places, our service providers may able provide location within few meters. however, in other areas might more that. locations returned our service providers estimates , not guarantee accuracy of locations provided. please not use information emergencies. use common sense.
source location-aware browsing - how accurate locations
so if 1 wants find accurate gps coordinates 1 should use either gps devices or html5 geolocation api in mobile devices comes gps hardware installed, 1 exact coordinates
Comments
Post a Comment