【问题标题】:How to fetch latitude,longitude or address from photo which is taken from camera如何从相机拍摄的照片中获取纬度、经度或地址
【发布时间】:2015-06-12 05:46:44
【问题描述】:

我想从相机拍摄的照片中获取纬度、经度或地址,我使用了链接iPhone Get UIImagePickerController Lat/Lng

我使用的代码是,

这就是我们处理选择器提供的信息的方式:

- (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];

    }
}

但是位置变量返回 nil。如何从相机拍摄的照片中提取位置或地址。

【问题讨论】:

  • @Ani Prasad。实际上我只关注了那个链接。如果你观察到有相同的代码
  • 照片本地化了吗?如果你去 Photos.app 并选择它,它是本地化的吗?例如,从(FaceBook、WhatsApp 等)获取的屏幕截图或照片可能未本地化。

标签: ios objective-c


【解决方案1】:

我已使用以下代码从相机图片中获取位置..希望这对您有所帮助

 - (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];

}
 }

【讨论】:

  • 我必须在我的代码中在哪里使用此方法?请澄清一下
  • 代替 ALAssetsLibraryAssetForURLResultBlock 块
  • 我的asset.defaultRepresentation.metadata 数据为零
  • 能给个简单又甜美的代码示例吗?
  • 我刚刚看了一眼。在元数据中没有 {GPS} 键或纬度和经度。应用您的代码后,我会得到它吗?
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 2018-01-20
  • 2013-07-04
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 2013-07-24
相关资源
最近更新 更多