地图注释的许多问题

时间:2018-03-28 10:42:29

标签: ios swift mkmapview mkannotationview

我有几个问题应该是一个非常简单的代码。目标是从数据库中读取项目并将其作为地图中的引脚。如果该项目已标记为收藏,则该引脚应为不同的颜色。

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

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

滚动地图时,第一针会随机丢失标题。

第二个最喜欢的(绿色)很少呈现绿色。 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
    }
}

查看控制器

    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%一致的。

  •   

    创建新视图:SEMINOLE DIALYSIS CENTER

  • 创建新视图:LAKE SEMINOLE DIALYSIS
  •   

    创建新视图:RAI CARE CENTERS - LARGO

  •   

    创建新视图:BAY BREEZE DIALYSIS CLINIC INC

  • 创建新视图:RENVIVA DIALYSIS CENTER OF CLEARWATER,LLC
  •   

    创建新视图:FKC - BELLEAIR DIALYSIS CENTER

  •   

    创建新视图:RAI CARE CENTERS - CLEARWATER

  • 创建新视图:FMC - BELLEAIR HOME THERAPIES
  •   

    创建新视图:CORVA GULF COAST DIALYSIS CENTER

  • 创建新视图:GULF BREEZE DIALYSIS CENTER
  •   

    为:BMA - CLEARWATER创建新视图

  • 创建新视图:RAI-US 19 NORTH-CLEARWATER

以下是显示上述结果的捕获

这是一个初始渲染。这次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 CLEALWATER DIALYSIS CENTER,LLC是蓝色
  • RAI CARE CENTERS - CLEARWATER是蓝色
  • FKC - BELLEAIR DIALYSIS CENTER是蓝色的
  • FMC - BELLEAIR HOME THERAPIES是蓝色的
  • GULF BREEZE DIALYSIS CENTER是蓝色的
  • BAY BREEZE DIALYSIS CLINIC INC是蓝色的
  • RAI-US 19 NORTH-CLEARWATER是蓝色的
  • CORVA GULF COAST DIALYSIS CENTER为蓝色

1 个答案:

答案 0 :(得分:0)

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

    ...
    // 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
    }
    ...