我想使用谷歌地理编码来查找带有名称的地点纬度经度列表,我没有在iPhone中使用此功能的经验,但在Android中只需调用网址即可。但是当我在 iPhone 中尝试相同的操作时,它给我错误“请求被拒绝”。指导我如何将其集成到 iPhone 中,或者我应该使用像 BSForwardGeocoder 这样的库,我正在这样做
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", locationString];
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *wurl = [NSURL URLWithString:url];
NSDictionary *dict = [Json objectWithUrl:wurl];
NSLog(@"%@", dict);
它打印为
{
results = (
);
status = "REQUEST_DENIED";
}
最佳答案
检查这个
NSString *locationString = @"1600 Amphitheatre Parkway, Mountain View, CA";
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", locationString];
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *wurl = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL: wurl];
// Fail to get data from server
if (nil == data) {
NSLog(@"Error: Fail to get data");
}
else{
// Parse the json data
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
// Check status of result
NSString *resultStatus = [json valueForKey:@"status"];
// If responce is valid
if ( (nil == error) && [resultStatus isEqualToString:@"OK"] ) {
NSLog(@"%@", json);
}
}
关于iPhone 谷歌地理编码请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16539740/