简体   繁体   English

如何在iPhone中的地图视图中添加注释

[英]How to add annotation in Map view In Iphone

I want to place an annotation on the Map view from the address typed in a text field using the MapKit instead of longitude and latitude in iphone.Whether is it possible to do that?If yes,how. 我想使用MapKit而不是iphone中的经度和纬度从在文本字段中键入的地址在地图视图上添加注释。是否可以这样做? Please provide some sample code to do this. 请提供一些示例代码来执行此操作。 Thanx in advance 提前感谢

You have to get the lat/long via some manner (geocoding). 您必须通过某种方式(地理编码)获取经度/纬度。 Here's an example I found on an excellent MKMapView tutorial: 这是我在出色的MKMapView教程中找到的示例:

NSString * address = @”1600 Pennsylvania Ave, Washington DC”; // address here

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@”200″]) {
  latitude = [[listItems objectAtIndex:2] doubleValue];
  longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
  //Error handling
}

From http://www.invasivecode.com/2009/07/adding-maps-to-your-iphone-app-mapkit/ http://www.sivecode.com/2009/07/adding-maps-to-your-iphone-app-mapkit/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM