Swift MapView 未向 Map 添加注释

标签 swift annotations mkmapview

所以对于我的 mapView,我通过像这样的全局数组传递经度和纬度来从另一个 VC 接收坐标:

将纬度和经度添加到数组中:

var location_one: [String: Any] = ["title": eventName, "latitude": latitude, "longitude": longitude]
    locations.append(location_one)
    if let tbc = self.tabBarReference {
        if let map = tbc.viewControllers?[1] as? MapVC {
            map.loadViewIfNeeded()
            map.add(newLocation:location_one)
        }
     }

接收MapViewVC上的坐标:

    func add(newLocation location_one:[String:Any]) {
        let momentaryLat = (location_one["latitude"] as! NSString).doubleValue
        let momentaryLong = (location_one["longitude"] as! NSString).doubleValue
        let annotation = MKPointAnnotation()
        annotation.title = location_one["title"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)
        map.addAnnotation(annotation)

        //        self.map.centerCoordinate = annotation.coordinate
    }

查看注释代码:

func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {



    let identifier = "pinAnnotation"
    var annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
    }

    annotationView?.annotation = annotation
    return annotationView
}

坐标正在正确传输并且正在调用 func add(使用 break 语句验证),但是当我转到模拟器中的 MapView 时,未放置注释。我不知道发生了什么,因为这在我测试的另一个应用程序中运行正常,但不知道是什么导致它搞砸了。

我也有代码可以找到用户的当前位置并根据该位置放置一个图钉...也许这会弄乱添加坐标代码?

最佳答案

我建议仔细检查 momentaryLat 的数值和 momentaryLong ,因为可能由于解析问题,它们不是您认为的那样(例如,确保它们不是 0.0,在大西洋!)。我们得看看是什么 location_one["latitude"]值是在您将其转换为 NSString 之前并从中提取 double 值。将其分解为单独的步骤,问题很可能会突然出现。


你的 viewFor看起来不错。不相关,我建议把 annotationView?.annotation = annotationelse里面前面的条款 if语句(如果您刚刚使用相同的 annotation 创建了 MKPinAnnotationView,则无需设置 annotation ... 只有在重用注释 View 时才需要这样做)。而且我通常还会检查以确保注释不是 MKUserLocation因为我们通常让 map View 为我们呈现。

func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }   // let the OS show user locations itself

    let identifier = "pinAnnotation"
    var annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

但这两个观察结果都与您的注释缺失问题无关。


请记住,viewForAnnotation (或 Swift 3 中的 viewFor )方法仅在 map 的一个或多个 annotations 被调用时被调用。有一个 coordinate属于 map 的可见部分,因此它得出结论,它需要呈现关联的注释 View 。因此,在 (a) map 尝试自行绘制和 (b) 一个或多个注释的坐标位于(或靠近) map 的可见部分之前,人们不会期望它被调用。

例如,如果您向 map 对象添加了 100 个注释,其中只有 10 个位于 map 的可见部分,则只会创建 10 个注释 View 。如果您滚动 map ,使 5 个离开 View ,另外 7 个滚动到 View 中,它会再调用 viewForAnnotation 7 次,每个新的可见注释调用一次。 viewForAnnotation 可能会成功地出列/重用滚出 View 的 5 个注释 View ,并且只会为另外两个注释 View 创建新的注释 View 。 (它比那更复杂,但这是基本思想。)

它类似于表/ Collection View ,其中仅为可见单元格创建单元格。因此,如果您的模型中有 100 个对象,但表格中只有 12 行可见,您将只创建 12 个单元格。当一行滚出 View 时,它可用于出队/重新使用以供将来滚动到 View 中的行使用。这是您将在整个 iOS 中看到的一种常见模式,其中计算量大的 View 对象仅在需要时创建,并尽可能重用。

关于Swift MapView 未向 Map 添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864720/

相关文章:

xcode - Swift - Xcode 7.3 中的闭包不能有关键字参数错误

ios - 在 Swift 中在 UIWebView 上方绘制

iphone - 将卫星 View 添加到具有 3D 功能的 MKMapKit IOS 6

swift - 我们可以将 willSet 和 didSet 与 getter 和 setter 一起使用吗?

swift - 在 tableview 中显示文档目录中的音频文件并在 swift 3 中选择行时播放音频文件

java - Spring : @Repository annotation for DAO

java - Spring MVC 如何转换 @RequestParam 值?

java - Spring : how to replace constructor-arg by annotation?

ios - 来自 MKPolyline 的纬度和经度点

swift - MapKit 渲染问题