1

I would like to fetch latitude,longitude or address from photo which is taken from camera ,I have used the link iPhone Get UIImagePickerController Lat/Lng

and the code which I used is,

So this is how we handle the info given by the picker:

- (void)imagePickerController:(UIImagePickerController *)picker
            didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoCamera) {
        // We'll store the info to use in another function later
        self.imageInfo = info;

        // Get the asset url
        NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

        // We need to use blocks. This block will handle the ALAsset that's returned: 
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
        {
            // Get the location property from the asset  
            CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
            // I found that the easiest way is to send the location to another method
            [self handleImageLocation:location];
        };
        // This block will handle errors:
        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
        {
            NSLog(@"Can not get asset - %@",[myerror localizedDescription]);
            // Do something to handle the error
        };


        // Use the url to get the asset from ALAssetsLibrary,
        // the blocks that we just created will handle results
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:url 
                       resultBlock:resultblock
                      failureBlock:failureblock];

    }
}

But the location variable returns nil. How extract the location or address from photo which is taken from camera.

Community
  • 1
  • 1

1 Answers1

0

I have used this below code for location from camera picture..Hope this helps you

 - (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingMediaWithInfo:(NSDictionary *)info
 {

    if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoCamera) {
    // We'll store the info to use in another function later
    self.imageInfo = info;

    // Get the asset url
    NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

    // We need to use blocks. This block will handle the ALAsset that's returned: 
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        // Get the location property from the asset  

        CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
        // I found that the easiest way is to send the location to another method

        self.lat =location.coordinate.latitude; //[[gpsdata valueForKey:@"Latitude"]floatValue];
        self.lng =location.coordinate.longitude;
        NSLog(@"\nLatitude: %f\nLongitude: %f",self.lat,self.lng);

        strLocation=[NSString stringWithFormat:@"La:%f Lo%f",self.lat,self.lng];

        NSLog(@"Can not get asset - %@",strLocation);

    };
    // This block will handle errors:
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can not get asset - %@",[myerror localizedDescription]);

    };

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:url 
                   resultBlock:resultblock
                  failureBlock:failureblock];

}
 }
soumya
  • 3,801
  • 9
  • 35
  • 69