我正在尝试让 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 是一个现在不相关的自定义类(它甚至没有到达那里......)
感谢您提供的任何帮助。