从Google Places Photo JSON将图像返回到imageView

时间:2014-01-08 19:17:17

标签: ios iphone objective-c google-places-api google-places

我希望从Google地方API获取照片,并将它们存储到URL或图像中,以便在单独的视图控制器中显示给用户。

照片json似乎为每个位置返回此内容:

    photos =  (
                {
                   height = 466;
                   "html_attributions" = ();
                   "photo_reference" = "CnRnAAAAgBUy_mqt9WglYJvS2v8XBfw5ER1U8Wn7DWvfoWI4P78w8_ZAsLQeFSYescNYE1NVgkV50jJE7SYBxdZuOmGP4jWGyCLobCd2nyEDbeB9lg1JU7KYV0o57i-OQTROIwj9ZwzWG03aUOypMjA_7fXD8BIQqbC5J0daROt_LqztWmz-xhoU20meJb50VyAE-zqkp9Jzu1Fegfs";
                   width = 695;
                }
             );

根据谷歌地方照片文档,它说:

https://developers.google.com/places/documentation/photos

无法找到能够从json获取此示例的示例,并将其存储为我是字符串,因为url不是指向图像或任何内容的直接链接。

编辑:收到错误

[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x13258510
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x13258510'

更新代码:

-(void)plotPositions:(NSArray *)data {

    for (id<MKAnnotation> annotation in _mapView.annotations) {
        if ([annotation isKindOfClass:[MapPoint class]]) {
            [_mapView removeAnnotation:annotation];
        }
    }
    //  Loop through the array of places returned from the Google API.
    for (int i=0; i<[data count]; i++) {

        NSDictionary* place = [data objectAtIndex:i];

        NSDictionary *geo = [place objectForKey:@"geometry"];

        NSDictionary *loc = [geo objectForKey:@"location"];

        // Getting Photo Reference Details - ADDED NOW RECIEVING ERROR?
        NSDictionary *photoDict = [place objectForKey:@"photos"];
        NSString *photoRef = [photoDict objectForKey:@"photo_reference"];

         NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/photo?photoreference=%@&key=%@&sensor=false&maxwidth=320", photoRef, kGOOGLE_API_KEY];

        NSLog(@"TESTING: %@", url);


        NSString *name=[place objectForKey:@"name"];
        NSString *vicinity=[place objectForKey:@"vicinity"];


        CLLocationCoordinate2D placeCoord;
        placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
        placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];

        MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity rating:rating coordinate:placeCoord];

        [_mapView addAnnotation:placeObject];
    }
}

2 个答案:

答案 0 :(得分:2)

JSON中的photo_reference属性是一个参数,您可以将其用作放置照片请求的参数。

在您引用的网址(https://developers.google.com/places/documentation/photos)中,请参阅“放置照片请求”部分,以正确方式实际构建请求图像的网址。

根据您的上述代码,您不会处理返回的照片数组。假设你想要第一张照片:

NSDictionary *photoDict = [[place objectForKey:@"photos"] objectAtIndex:0];
NSString *photoRef = [photoDict objectForKey:@"photo_reference"];

NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/photo?photoreference=%@&key=%@&sensor=false&maxwidth=320", photoRef, kGOOGLE_API_KEY];

答案 1 :(得分:0)

要获取图像的网址,您需要使用他们称之为“照片请求”的请求来请求它。它使用起来非常简单,您需要将照片参考传递到您的网址中。 带有照片参考的示例网址

注意 每个密钥都由“&amp;”

分隔

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CoQBegAAAFg5U0y-iQEtUVMfqw4KpXYe60QwJC-wl59NZlcaxSQZNgAhGrjmUKD2NkXatfQF1QRap-PQCx3kMfsKQCcxtkZqQ&sensor=true&key=AddYourOwnKeyHere

必填参数

key — Your application's API key. This key identifies your application for purposes of quota management. Visit the APIs Console to create an API Project and obtain your key.
photoreference — A string identifier that uniquely identifies a photo. Photo references are returned from either a Place Search or Place Details request.
sensor — Indicates whether or not the Place Photo request came from a device using a location sensor (e.g. a GPS). This value must be either true or false.
maxheight or maxwidth — Specifies the maximum desired height or width, in pixels, of the image returned by the Place Photos service. If the image is smaller than the values specified, the original image will be returned. If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio. Both the maxheight and maxwidth properties accept an integer between 1 and 1600.

更新运行此代码。

-(void)plotPositions:(NSArray *)data {

for (id<MKAnnotation> annotation in _mapView.annotations) {
    if ([annotation isKindOfClass:[MapPoint class]]) {
        [_mapView removeAnnotation:annotation];
    }
}
//  Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++) {

    NSDictionary* place = [data objectAtIndex:i];

    NSDictionary *geo = [place objectForKey:@"geometry"];

    NSDictionary *loc = [geo objectForKey:@"location"];

    // Getting Photo Reference Details - ADDED NOW RECIEVING ERROR?
    NSArray *allPhotosDictionaries = [place objectForKey:@"photos"];
    NSDictionary *photoDict;
    if (allPhotosDictionaries.count > 0) {
        photoDict = [allPhotosDictionaries objectAtIndex:0];

    }else {
        photoDict = [[NSDictionary alloc] init];

    }

    NSString *photoRef = [photoDict objectForKey:@"photo_reference"];

    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/photo?photoreference=%@&key=%@&sensor=false&maxwidth=320", photoRef, kGOOGLE_API_KEY];

    NSLog(@"TESTING: %@", url);


    NSString *name=[place objectForKey:@"name"];
    NSString *vicinity=[place objectForKey:@"vicinity"];


    CLLocationCoordinate2D placeCoord;
    placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
    placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];

    MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity rating:rating coordinate:placeCoord];

    [_mapView addAnnotation:placeObject];
}
}