在iOS 5中将CLPlacemark放在地图上

Ste*_*lth 10 mapkit mkmapview ios ios5

在iOS 5中,有一种转发地理编码地址的新方法(将地址转换为1 Infinite Loop,CA,USA到lat/lang地址).关于这方面的更多信息在这里.

有人试图将前向地理编码的CLPlacemark对象放在MKMapView上吗?我在地理编码后有一个CLPlacemark对象,但不知道如何将它放在地图上.

我会很感激任何类型的帮助.到目前为止谷歌没有任何帮助.

Ste*_*lth 24

除了选定的答案之外,还有这种方法可以为mapView添加注释以进行前向地理编码.CLPlacemark对象可以直接转换为MKPlacemark并添加到mapview.像这样:

MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
[self.mapView addAnnotation:placemark];
Run Code Online (Sandbox Code Playgroud)

这是前向地理编码的完整示例.

 NSString *address = @"1 Infinite Loop, CA, USA";
 CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:address 
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     // Check for returned placemarks
                     if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         // Create a MLPlacemark and add it to the map view
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                         [self.mapView addAnnotation:placemark];
                         [placemark release];
                     }
                     [geocoder release];
                 }];
Run Code Online (Sandbox Code Playgroud)


小智 9

A CLPlacemark没有实现MKAnnotation协议,因此您仍然需要创建自己的注释类,或者您可以使用MKPointAnnotation.地标的坐标属于其location财产.

例如:

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = placemark.location.coordinate;
pa.title = ABCreateStringWithAddressDictionary(placemark.addressDictionary, YES);
[mapView addAnnotation:pa];
[pa release];  //remove if using ARC
Run Code Online (Sandbox Code Playgroud)

您可以title从地标设置您想要的任何内容,但是如示例所示,一种可能性是使用地址簿UI框架从地标提供的地址字典生成地址字符串.