【问题标题】:Putting a CLPlacemark on Map in iOS 5在 iOS 5 中的地图上放置 CLPlacemark
【发布时间】:2011-11-27 03:49:37
【问题描述】:

在 iOS 5 中,有一种转发地理编码地址的新方法(将地址如 1 Infinite Loop, CA, USA 转换为 lat/lang 地址)。 More info on this is here.

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

我将不胜感激任何类型的帮助。到目前为止,谷歌没有任何帮助。

【问题讨论】:

    标签: ios ios5 mkmapview mapkit


    【解决方案1】:

    除了选中的答案外,还有这种方式可以给mapView添加注解进行正向地理编码。 CLPlacemark 对象可以直接转换为 MKPlacemark 并添加到地图视图中。 像这样:

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:placemark];
    [self.mapView addAnnotation:placemark];
    

    这是正向地理编码的完整示例。

     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];
                     }];
    

    【讨论】:

      【解决方案2】:

      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
      

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

      【讨论】:

      • 谢谢!这非常有帮助!
      • 我还通过今年 (2011) WWDC 的视频找到了另一种方法。仅供参考。
      • 在使用 ABCreateStringWithAddressDictionary...2 时,我一直看到奇怪的微小内存泄漏...2 泄漏每次出现总共 192 字节。有没有其他人遇到过这个问题?
      • @AndrewPK 在 Xcode 中运行分析引用 ABCreateStringWithAddressDictionary 作为潜在泄漏的来源,所以肯定有一些奇怪的事情发生。
      【解决方案3】:

      在 Swift 3.0 中

       let pm = placemarks! as [CLPlacemark] // this is Clpmacemark
       let placemark = MKPlacemark.init(placemark: pm)
       self.mapview.addAnnotation(placemark)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多