How Do I Get The Correct Latitude and Longitude From An Uploaded iPhone Photo?

前端 未结 5 1673
难免孤独
难免孤独 2020-12-03 00:27

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

相关标签:
5条回答
  • 2020-12-03 00:33

    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);
    
    0 讨论(0)
  • 2020-12-03 00:34

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

    0 讨论(0)
  • 2020-12-03 00:48

    i think you should grab the value using following:

     CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
    
    0 讨论(0)
  • 2020-12-03 00:49

    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];
    
        };
    
    0 讨论(0)
  • 2020-12-03 00:52

    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.

    0 讨论(0)
提交回复
热议问题