13

My problem actually seems rather silly... I am writing an iPhone application that uses MKMapKit. The app grabs the EXIF metadata from a provided geotagged photo. The problem is that the latitude and longitude coordinates that I retrieve, for example:

Lat: 34.25733333333334

Lon: 118.5373333333333

returns a location in China. Is there a regional setting that I am missing or do I need to convert the lat/long coordinates before using them?

Thank you all in advance for any help you can provide.

Here is the code I am using to grab the GPS data. You'll notice I am logging everything to the console so I can see what the values are:

void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)
{
    NSDictionary *metadata = asset.defaultRepresentation.metadata;
    NSLog(@"Image Meta Data: %@",metadata);
    NSDictionary *gpsdata = [metadata objectForKey:@"{GPS}"];
    self.lat = [gpsdata valueForKey:@"Latitude"];
    self.lng = [gpsdata valueForKey:@"Longitude"];
    NSLog(@"\nLatitude: %@\nLongitude: %@",self.lat,self.lng);
};

NSURL *assetURL = [mediaInfo objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL
         resultBlock:ALAssetsLibraryAssetForURLResultBlock
        failureBlock:^(NSError *error) {
        }];

UIImage *img = [mediaInfo objectForKey:@"UIImagePickerControllerEditedImage"];
previewImage.image = nil;
self.previewImage.image = img;

NSData *imageData = UIImagePNGRepresentation(img);
if ([imageData length] > 0) {
    self._havePictureData = YES;

}
Leachy Peachy
  • 1,112
  • 2
  • 17
  • 29

5 Answers5

14

i think you should grab the value using following:

 CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
Allen
  • 6,505
  • 16
  • 19
  • 1
    Hmm. This is not working for me. I take the photo and, as soon as I get the image back via **UIImagePickerControllerDelegate** I check the assets. ALAssetPropertyLocation is *not* present, even with permission already granted to the app in question (and to the Camera app, FWIW), on iOS 5.x on an iPhone 4 and iPad 3rd gen. – Joe D'Andrea Jun 17 '12 at 11:00
2

While you can get the location from the asset per @Allen, it is also valid to get it from the GPS metadata as you were trying to do initially. I'm not 100% sure the asset library coordinate will be the same as the coord in the GPS metadata, it depends on how Apple stores this coord. For example, if you are using a timestamp, the Asset library timestamp is different than the EXIF creation date (a different topic, admittedly).

In any case, the reason you have the coord wrong is b/c you also need to get the direction info as follows:

NSDictionary *metadata = asset.defaultRepresentation.metadata;
NSLog(@"Image Meta Data: %@",metadata);
NSDictionary *gpsdata = [metadata objectForKey:@"{GPS}"];
self.lat = [gpsdata valueForKey:@"Latitude"];
self.lng = [gpsdata valueForKey:@"Longitude"];

// lat is negative is direction is south
if ([[gpsdata valueForKey:@"LatitudeRef"] isEqualToString:@"S"]) {
    self.lat = -self.lat;
}

// lng is negative if direction is west
if ([[gpsdata valueForKey:@"LongitudeRef"] isEqualToString:@"W"]) {
    self.lng = -self.lng;
}

NSLog(@"\nLatitude: %@\nLongitude: %@",self.lat,self.lng);
XJones
  • 21,959
  • 10
  • 67
  • 82
2

Are you sure you're not missing a minus sign on that 118? 34.257, -118.5373 is nicely inside Los Angeles, California.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • Thank you for looking at this. It's so strange. The lat/lng I get from any photos don't have any minus sign. I'm worried that if I just automatically assume a minus sign, I might get inaccurate results. – Leachy Peachy Feb 16 '12 at 21:50
  • Post the code you're using to get the lat/lng. It may be performing a cast which is removing the sign. – Tim Feb 16 '12 at 21:52
0

This also will works,

void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)
    {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        NSDictionary *metadata = rep.metadata;

        NSMutableDictionary *GPSDictionary = [[[metadata objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy] autorelease];

    };
Venk
  • 5,949
  • 9
  • 41
  • 52
  • This will not work correctly. It doesn't include '-' sign for either lat or long values. so unfortunately gives wrong result. – Hemant Sharma Oct 08 '14 at 10:13
0

I believe that the reason that there isn't a negative sign is because of the metadata: exif:GPSLongitudeRef: W which (I believe) means that there should be a negative sign in front of the longitude since it is referencing the western hemisphere. I believe that this also applies to the latitude but with exif:GPSLatitudeRef: N for Northern and Southern hemispheres. Hope that this helped. Just realized this is exactly what @XJones said. Metadata using ImageMagick.

R167
  • 1
  • 3