ios - map 注释有很多问题

标签 ios swift mkmapview mkannotationview

对于应该是一段非常简单的代码,我有几个问题。目标是从数据库中读取项目并将它们固定在 map 上。如果项目已被标记为收藏夹,图钉应该是不同的颜色。

第一个问题是不是所有的项目都被渲染。在示例中,我将使用查询返回的 12 个结果,并且我已验证每个项目都创建了一个 MKAnnotation,并且每个注释都调用了 ViewFor。

除了不显示所有引脚外,还有另外两个问题。

滚动 map 时,第一个 Pin 图会随机丢失其标题。

其次最喜欢的(绿色)很少呈现绿色。 80% 的时间它以标准蓝色出现。我再次确认 MKMarkerAnnotationView 颜色设置正确。

鉴于所有这些问题,我不得不得出结论,我在做一些根本上非常错误的事情。这很奇怪,因为这看起来非常简单。

class FacilityMarker: NSObject, MKAnnotation {

// title and subtitle are from the MKAnnotation protocol
var coordinate:     CLLocationCoordinate2D
var title:          String?
var address:        String
var phone:          String
var providerNumber: String
var favorite:       Bool
var subtitle:       String? {
    get {
        return phone
    }
}

View Controller

    class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    mapView.showAnnotations(mapView.annotations, animated: true)


// Create Annotations for all the facilities that are now visible on map
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let edgePoints = mapView.edgePoints()

    let minLat = edgePoints.ne.latitude < edgePoints.sw.latitude ? edgePoints.ne.latitude : edgePoints.sw.latitude
    let maxLat = edgePoints.ne.latitude > edgePoints.sw.latitude ? edgePoints.ne.latitude : edgePoints.sw.latitude
    let minLong = edgePoints.ne.longitude < edgePoints.sw.longitude ? edgePoints.ne.longitude : edgePoints.sw.longitude
    let maxLong = edgePoints.ne.longitude > edgePoints.sw.longitude ? edgePoints.ne.longitude : edgePoints.sw.longitude
    let visibleCitiesReqeuest =
        managedObjectModel.fetchRequestFromTemplate(withName: "FetchByCoordinates", substitutionVariables: ["minLat" : minLat, "minLong" : minLong, "maxLat" : maxLat, "maxLong" : maxLong])
    do {
        let facilities = try CoreDataHelper.shared.persistentContainer.viewContext.fetch(visibleCitiesReqeuest!) as! [FacilityMO]
       for facility in facilities {
            let facilityMarker = FacilityMarker(name: facility.facilityName!, address: facility.addressLine1!, location: facility.location!, phone: facility.phoneNumber!, providerNumber: facility.providerNumber!, favorite: facility.favorite)   
            mapView.addAnnotation(facilityMarker)
        }
    } catch {
        let alertController = UIAlertController(title: "No Facilities", message: "There are no Facilities within the visible map area", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil))
        self.present(alertController, animated: true, completion: nil)
    }
}

// Put the pins in the map
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annotation = annotation as? FacilityMarker else { return nil}

    let identifier = "facility"
    var view: MKMarkerAnnotationView
    if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as?
        MKMarkerAnnotationView {
        dequeuedView.annotation = annotation
        view = dequeuedView
    } else {
        print("CREATING NEW View for: \(annotation.title!)")
        view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
    }

    // Set the Colors
    if (annotation.favorite) {
        view.markerTintColor = .green
        view.tintColor = .green
    } else {
        view.markerTintColor = .blue
        view.tintColor = .blue
    }
    return view
}

这是查询返回的实际数据。只有突出显示的项目才会被渲染。 这在所有测试中都是 100% 一致的。

  • CREATING NEW View for: SEMINOLE DIALYSIS CENTER

  • 为以下项目创建新 View :塞米诺尔湖透析
  • CREATING NEW View for: RAI CARE CENTERS - LARGO

  • CREATING NEW View for: BAY BREEZE DIALYSIS CLINIC INC

  • 为 CLEARWATER, LLC 的 RENVIVA 透析中心创建新 View
  • CREATING NEW View for: FKC - BELLEAIR DIALYSIS CENTER

  • CREATING NEW View for: RAI CARE CENTERS - CLEARWATER

  • 为 FMC - BELLEAIR HOME THERAPIES 创造新视野
  • CREATING NEW View for: CORVA GULF COAST DIALYSIS CENTER

  • 为 GULF BREEZE 透析中心创建新 View
  • CREATING NEW View for: BMA - CLEARWATER

  • 为 RAI-US 19 NORTH-CLEARWATER 创建新 View

以下是显示上述结果的截图

这是一个初始渲染。这次 BMA 引脚是蓝色的,它被设置为绿色。 Initial Render View Tint Wrong

这次滚动后 BMA 色调是正确的。没有什么时候是正确的模式。 Sometimes Tint Works. Might be on initial render or maybe after scrolling.  No pattern detected

更多的滚动只是为了增加更多的奇怪性,有时一个或多个注释不会呈现它们的标题属性 Sometimes Annotation Title disappears after scrolling

请求调试的输出:

  • RAI CARE CENTERS - LARGO 是蓝色的
  • BMA - CLEARWATER 是绿色的
  • RENVIVA DIALYSIS CENTER OF CLEARWATER, LLC 是蓝色的
  • RAI 护理中心 - CLEARWATER 是蓝色的
  • FKC - BELLEAIR 透析中心是蓝色的
  • FMC - BELLEAIR HOME THERAPIES 是蓝色的
  • GULF BREEZE 透析中心是蓝色的
  • BAY BREEZE DIALYSIS CLINIC INC 是蓝色的
  • RAI-US 19 NORTH-CLEARWATER 是蓝色的
  • CORVA GULF COAST DIALYSIS CENTER 是蓝色的

最佳答案

尝试以下调试代码。你能显示输出吗?

    ...
    // Set the Colors
    if (annotation.favorite) {
        print(annotation.title, "is green")
        view.markerTintColor = .green
        view.tintColor = .green
    } else {
        print(annotation.title, "is blue")
        view.markerTintColor = .blue
        view.tintColor = .blue
    }
    ...

关于ios - map 注释有很多问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532389/

相关文章:

iphone - 在 uitextview 周围添加自定义边框,但几乎没有超出其范围

ios - 添加 subview (标签)时如何实现可调整大小的父高度?

ios - 通过字符串 Xcode 标识符访问 UILabel

ios - 使用 iOS 平移手势突出显示网格中的按钮

arrays - 如何检查值是否介于存储在数组中的两个值之间

iphone - 如何从 MKMapView 中删除所有注释而不删除蓝点?

ios - 无法弄清楚如何使用 CLPlacemark 将我的当前位置正确地嵌入到消息正文中

ios - CFRelease(addressBook) 使我的 iOS 应用程序崩溃

iphone - 在 map View 中实现获取方向

iphone - 根据道路计算两个位置之间的距离