对于一段非常简单的代码,我有几个问题。目标是从数据库中读取项目并将它们固定在 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
CREATING NEW View for: RAI CARE CENTERS - LARGO
CREATING NEW View for: BAY BREEZE DIALYSIS CLINIC INC
CREATING NEW View for: FKC - BELLEAIR DIALYSIS CENTER
CREATING NEW View for: RAI CARE CENTERS - CLEARWATER
CREATING NEW View for: CORVA GULF COAST DIALYSIS CENTER
CREATING NEW View for: BMA - CLEARWATER
以下是显示上述结果的捕获
这是一个初始渲染。这次 BMA 引脚是蓝色的,它被设置为绿色。
这次滚动后,BMA 色调是正确的。什么时候是正确的,没有规律可循。
更多的滚动,只是为了增加更多的陌生感,有时一个或多个注释不会呈现它们的标题属性
请求调试的输出:
试试下面的调试代码。你能显示输出吗?
...
// 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |