0

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!

j0k
  • 22,600
  • 28
  • 79
  • 90
Derek Dub
  • 53
  • 2
  • 11
  • do you want to calculate fly or drive distance? witch google map api you can get maps distance https://developers.google.com/maps/documentation/distancematrix/?hl=en – Eugen Feb 25 '13 at 06:36
  • I'm not sure how to answer that. I guess drive distance. Most of the codes I've found are in meters and then I'll convert them to feet which is not the issue. I just haven't found a php function that takes two known latitude and longitude values and returns the distance between them. I'm also thinking it might be the javascript function I'm using to get the lat and long in the first place. I think it's using html5's capabilities which may not be that accurate. do you know of another js function that gets the clients lat and long based on their mobile device's location? – Derek Dub Feb 25 '13 at 06:53
  • so I figured out that the php functions that calculate the distance between two different latitude and longitudes that I've been using are actually correct. My main problem now is using a function in javascript that gets the accurate latitude and longitude of the user when the javascript function is called. do you know of anything in js that is reliable? – Derek Dub Feb 25 '13 at 07:11

0 Answers0