0

我正在尝试让 google places API 在我的 iPhone 项目上工作。现在,大约一个小时前我让它工作了,但我似乎无法弄清楚我做了什么让它停止工作。任何帮助,将不胜感激。

这是我到目前为止所拥有的:

- (NSString *)searchString {
    // this mutable string allows me to dynamically create the search string
    // we start with the static part of the api search URL
    NSMutableString *result = [NSMutableString stringWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location="];

    // since I need to get the user's location, I need to create a location manager
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];

    // we need to now update the current location,
    // otherwise there will be no coordinates
    [locationManager startUpdatingLocation];

    // now that it's updated, we stop it because I
    // am not tracking anything
    [locationManager stopUpdatingLocation];

    // this appends the lattitude/longitude, as double values, into the URL
    [result appendFormat:@"%g,%g", [[locationManager location] coordinate].latitude, [[locationManager location] coordinate].longitude];

    // release the location manager for memory management
    [locationManager release];

    // if a filter is present, add the keyword item to try to filter
    // the results
    if([[self filterString] length] > 0) {
        [result appendFormat:@"&keyword=%@", filterString];
    }

    // add the rest of the validated URL now
    //[result appendString:@"&types=food|meal_delivery|meal_takeaway|restaurant&rankby=distance&sensor=true&key=AIzaSyBmO_f6h4_Q0xArw6tdxUF7TH7rZpaiFfQ"];
    [result appendString:@"&types=food&rankby=distance&sensor=true&key=mykey"];

    // log the result for testing
    NSLog(@"Completed Search String: %@", result);

    return result;

}

现在,当我查看日志时,将“完成的搜索字符串”复制到 Safari 中,它会显示我需要的结果。

但如果我使用以下代码,应用程序会挂起:

- (void)performSearch {

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self searchString]]]; // hangs on this line!

    NSDictionary *jsonDictionary = [data objectFromJSONData];
    NSArray *resultsArray = [jsonDictionary objectForKey:@"results"];

    currentList = [ARGooglePlace placesWithArray:resultsArray];

    [self.tableView reloadData];
}

我想我应该提到我正在使用 JSONKit 来进行 JSON 解析。此外,ARGooglePlace 是一个现在不相关的自定义类(它甚至没有到达那里......)

感谢您提供的任何帮助。

4

1 回答 1

0

从 searchString 方法中拉出位置管理器和纬度/经度...只需将其放入 performSearch 方法中即可。相反,将从位置管理器接收到的纬度/经度传递给 searchString。

这听起来像是您的位置经理的时间问题。它本来可以在早期工作的 b/c 位置管理器之前缓存了位置数据......并且能够获取正确的坐标。

2+ 可能的情况:

1)位置管理器的问题没有更新它的坐标,只是挂在那里 2)谷歌网站是罪魁祸首(可能加载了太多数据??)

将位置管理器从 searchString 方法中提取出来将有助于隔离原因......而且人们也可以直接传递 lat/long 值来测试谷歌网站。

于 2012-04-17T20:46:26.770 回答