Xcode iOS 4.2.1 - >给地图查看显示地址的地址?
问题描述:
可能重复:
Forward Geocode Example using CLGeocoderXcode iOS 4.2.1 - >给地图查看显示地址的地址?
我有一个plist中的全名及其adrresses的。我希望我的iOS应用程序在地图上显示一个包含他所选人员地址的密码。我可以将地址导入地图视图并获得一个PIN吗?如何??谢谢。
答
这就是所谓的前向地理编码,并没有内置的API来为你做这件事。您需要使用外部服务,您使用的服务实际上取决于您的应用程序。
我已经使用过Google的服务。 http://code.google.com/apis/maps/documentation/geocoding/
这是一个非常简单的API,但基于您的应用程序许可证有限制。我知道还有其他一些服务,但我会把它作为练习留给你。
答
使用CLGeocoder类地址转换为纬度/经度坐标:
Forward Geocode Example using CLGeocoder
你有更准确的地址,更好的结果应该是这样用确切的地址,你应该得到非常精确的点。
答
另一种选择是使用Google的地理编码Web服务,只需将地址字符串传递给此函数,您将获得包含纬度&经度的CLLocationCoordinate2D。
现在它抓住第0个索引处的位置,这是最接近的匹配结果。注销结果以查看来自Google的JSON响应。
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=true&address=%@", esc_addr];
CLLocationCoordinate2D center;
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result)
{
//NSLog(@"LOC RESULT: %@", result);
NSError *e;
NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &e];
NSArray *resultsArray = [resultDict objectForKey:@"results"];
if(resultsArray.count > 0)
{
resultDict = [[resultsArray objectAtIndex:0] objectForKey:@"geometry"];
resultDict = [resultDict objectForKey:@"location"];
center.latitude = [[resultDict objectForKey:@"lat"] floatValue];
center.longitude = [[resultDict objectForKey:@"lng"] floatValue];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"No locations were found using, please try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
//goes through each result
/*for(NSDictionary *dict in resultsArray)
{
resultDict = [dict objectForKey:@"geometry"];
resultDict = [resultDict objectForKey:@"location"];
}*/
}
return center;
}
`[[UIApplication的sharedApplication]的OpenURL:[NSURL URLWithString:[的NSString stringWithFormat:@ “http://maps.google.com/maps?q=%@&z=15”,yourAddress]]]; `会起作用,然而它在你发送的内容上有一点限制。您始终可以通过在Web浏览器中使用完全相同的URL进行测试。我怀疑你会得到任何结果,如果你包含一个名字,但一个完整的地址将打开Maps.app就好,并正确地放置引脚。 – runmad 2011-01-25 03:43:00