I have a really long question and I'm hoping one or many people can help. I'm having trouble with a function that I found online which, given two latitude and longitude coordinates, will find the distance between the two. the function is as follows:
<script>
function handler(location) {
var latitude = location.coords.latitude;
var longitude = location.coords.longitude;
document.getElementById('lat').value = latitude;
document.getElementById('long').value = longitude;
}
navigator.geolocation.getCurrentPosition(handler);
</script>
Then I have a button at the bottom along with hidden fields:
<input type="submit" id="button" name='submit' value="Bounce" onClick="handler(location)">
<input type='hidden' name='latitude'id="lat">
<input type='hidden' name='longitude' id="long">
so when I click the submit button, it calls the function, then sets the values of the hidden fields to the latitude and longitude for a post to PHP upon submission. However this function(along with php and mysql code) inserts slightly different coordinates each time when running the webpage from a phone...even if the phone is not moved at all.
1.Does anyone know of another javascript/jquery/whatever that can get the lat and long of a user on the client side and is actually accurate and consistent?
I'm also trying to find the distance between two latitude and longitude values i.e. lat1,long1,lat2,long2. I've found many php calculations that supposedly find this distance by just simply passing through lat1,long1,lat2,long2 through a function and returning a distance in meters however every time, it returns a super huge number when the two devices have nearly identical latitude and longitude coordinates and are right beside each other when the web page is called. Here are some php codes that I found that just aren't returning the right value:
function distanceGeoPoints ($lat1, $lng1, $lat2, $lng2) {
$earthRadius = 3958.75;
$dLat = deg2rad($lat2-$lat1);
$dLng = deg2rad($lng2-$lng1);
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *sin($dLng/2) * sin($dLng/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$dist = $earthRadius * $c;
// from miles
$geopointDistance = $dist;
return $geopointDistance;
}
function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo){
// convert from degrees to radians
$earthRadius = 6371000;
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$lonDelta = $lonTo - $lonFrom;
$a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
$angle = atan2(sqrt($a), $b);
return $angle * $earthRadius;
}
2.Has anyone else had this issue and/or may know what I'm doing wrong? do these calculations not accept negative lat or long values? 3.Does anyone know of a php function that works that hopefully I haven't heard of yet?
This was a really long post but any help on any of the parts that I had questions on would be greatly appreciated.
Thanks!