0

我正在使用 google places API,json 返回有效结果,在我的异步方法中,我为每个结果设置标记,方法结束,突然我得到以下线程 1:EXC_BAD_ACCESS 代码:2 地址:0x9243e8,在输出窗口中我得到以下信息: [71539:c07] -[__NSCFString CGImage]: unrecognized selector sent to instance 0xb19dd10

我不知道如何调试这个,或者在哪里寻找问题,我所有的参数看起来都不错。

谢谢

4

1 回答 1

0

我怀疑您正在做的就像评论者所说的那样,将返回的 JSON 对象视为不是的类型。具体来说,根据您的错误,您将字符串视为图像。通常,JSON 不包含实际的图像,所以当你反序列化它时,你不会得到图像。在 Google Places API 中,它不会向您发送图像。相反,它会向您发送图像的 URL。您必须使用从 Places API 获得的 URL 下载图像。

这是我使用的一些代码。此方法基于 a 从 API 获取地点列表,CLLocation并将其作为更容易消化NSArrayNSDictionary对象返回,每个对象都包含与地点有关的重要元数据:

-(NSArray*)getPlaceListForLocation:(CLLocation*)inLocation {

    NSString* requestStr = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=%@&location=%f,%f&radius=100&sensor=true",self.googleAPIKey,inLocation.coordinate.latitude,inLocation.coordinate.longitude];

    NSURL* requestURL = [NSURL URLWithString:requestStr];

    NSLog(@"Fetching place data with URL = %@", requestURL);

    NSData* requestData = [NSData dataWithContentsOfURL:requestURL];

    if ( nil != requestData ) {
        NSError* jsonError = nil;

        id resultsObj = [NSJSONSerialization JSONObjectWithData:requestData options:0 error:&jsonError];

        if ( nil == resultsObj ) {
            if ( nil != jsonError ) {
                NSLog(@"%@",jsonError.description);
            }

            return nil;
        }

        NSArray* itemList = [resultsObj objectForKey:@"results"];

        if ( nil == itemList || itemList.count == 0) {
            return nil;
        }

        NSMutableArray* placeList = [NSMutableArray arrayWithCapacity:itemList.count];

        for ( NSDictionary* item in itemList ) {
            NSString* name = [item objectForKey:@"name"];
            NSString* iconURL = [item objectForKey:@"icon"];
            NSString* vicinity = [item objectForKey:@"vicinity"];
            NSDictionary* geometryDict = [item objectForKey:@"geometry"];
            NSDictionary* locationDict = [geometryDict objectForKey:@"location"];

            NSMutableDictionary* itemDict = [NSMutableDictionary dictionaryWithCapacity:2];

            if ( nil != name ) {
                [itemDict setObject:name forKey:@"name"];
            }

            if (nil != iconURL ) {
                [itemDict setObject:iconURL forKey:@"iconurl"];
            }

            if ( nil != vicinity ) {
                [itemDict setObject:vicinity forKey:@"vicinity"];
            }

            if ( nil != locationDict ) {
                NSString* latitudeStr = [locationDict objectForKey:@"lat"];
                NSString* longitudeStr = [locationDict objectForKey:@"lng"];

                double latitude = [latitudeStr doubleValue];
                double longitude = [longitudeStr doubleValue];

                CLLocation* placeLoc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

                CLLocationDistance distance = [inLocation distanceFromLocation:placeLoc];

                [itemDict setObject:[NSNumber numberWithDouble:distance] forKey:@"distance"];
            }


            [placeList addObject:itemDict];
        }

        return placeList;
    }

    return nil;
}

NSArray然后我使用返回的数据源在表格视图中显示这些。为了显示图标,我使用类似MKNetworkImageView的东西来根据 URL 获取图标。

于 2013-08-26T07:14:11.640 回答