1

I'm using the php exif_read_data function to get the long and lat, then I'm converting them into a format the google can use, however they always seem to be a few miles out. The same country, just not the right position (iPhoto will give me the correct position, however). The code is as follows

public function get_location() {
    global $the_image;

    if(!empty($the_image)) {
      $exif = $this->get_exif();

      $degLong = $exif['GPSLongitude'][0];
      $minLong = $exif['GPSLongitude'][1];
      $secLong = $exif['GPSLongitude'][2];
      $refLong = $exif['GPSLongitudeRef'];

      $degLat = $exif['GPSLatitude'][0];
      $minLat = $exif['GPSLatitude'][1];
      $secLat = $exif['GPSLatitude'][2];
      $refLat = $exif['GPSLatitudeRef'];

      $long = $this->to_decimal($degLong, $minLong, $secLong, $refLong);
      $lat  = $this->to_decimal($degLat, $minLat, $secLat, $refLat);
      echo $long . "<br />" . $lat . "<br/>";

    } else {
      echo "Supply an image";
    }
}

  function to_decimal($deg, $min, $sec, $hem){
    $d = $deg + ((($min/60) + ($sec/3600))/100);
    return ($hem =='S' || $hem=='W') ?  $d*=-1 : $d;
  }

Can anyone see what would make it return the wrong data, is my to_decimal function wrong?

Luke Berry
  • 1,927
  • 4
  • 19
  • 32
  • Which package are you using? Can you please show how you extracted gps information in PHP? – ultrasamad Feb 05 '18 at 16:18
  • Hi @ultrasamad this is a really old question and shouldn't really be resurrected but I was using the PHP `exif_read_data` method found here http://php.net/manual/en/function.exif-read-data.php. No packages, just PHP. – Luke Berry Mar 05 '18 at 14:06

1 Answers1

3

You devide the minutes and seconds by 100, but well, you shouldn't:

$d = $deg + ((($min/60) + ($sec/3600))/100);

should be:

$d = $deg + (($min/60) + ($sec/3600));

one minute is 1/60 degree and a second is 1/60 minute.


for a more thorough answer, see here: PHP extract GPS EXIF data

Community
  • 1
  • 1
devsnd
  • 7,382
  • 3
  • 42
  • 50
  • Wow, I can't believe I didn't see that. I don't even know why I added that. Thank you, it is 3:40AM here - that might be why :) – Luke Berry Aug 15 '13 at 02:40
  • 1
    4:40 here, but yes, another pair of eyes makes all the difference sometimes :) – devsnd Aug 15 '13 at 02:48
  • This code is still not correct. Locations are still a few hundred miles out. Check out this example here by Gerald Kaszuba that works accurately: http://stackoverflow.com/questions/2526304/php-extract-gps-exif-data – Asa Carter Jan 13 '14 at 17:25
  • Thanks, I'll include the link in my answer so it can be found more easily, since this problem was rather a slip of the pen. – devsnd Jan 13 '14 at 21:49