不久前,我在我的地图应用程序中添加了一个搜索框,它使用最少的选项进行非常简单的地址搜索。
http://maps.googleapis.com/maps/api/geocode/json?address=sydney+grove&sensor=true
我刚刚使用当前屏幕视口添加了边界参数,如下所示
它在粘贴到浏览器时返回一个结果,但如果在代码中输入则始终返回 nil 结果(jsonResponse 始终等于 nil)
-(void) doGeocodingBasedOnStringUsingGoogle:(NSString*) searchString {
GMSCoordinateBounds* bounds=[[self datasource] searchBounds];
//CREATE LOOKUP STRING
NSString *lookUpString = [NSString
stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?
address=%@&bounds=%f,%f|%f,%f&sensor=true",
searchString,
bounds.southWest.latitude,
bounds.southWest.longitude,
bounds.northEast.latitude,
bounds.northEast.longitude];
lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" "
withString:@"+"];
//SEARCH FOR RESULTS
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];
if (jsonResponse) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];
self.searchResults = [jsonDict valueForKey:@"results"];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableview reloadData];
});
});
}
在我添加边界条件之前,这段代码很好,如果我删除也很好,所以我真的没有想法