【发布时间】:2017-12-21 08:09:11
【问题描述】:
我正在尝试制作一个 Uber 克隆,其中有很多乘客和司机的注释。我只想加载和显示属于当前显示的地图区域的注释,这样所有的注释都不会添加到地图视图中,并且不会占用太多内存。
我有两种类型的注释:司机和乘客。我有不同的图片作为他们的注释。
这是我的尝试:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? DriverAnnotation {
guard let dequeuedDriverAnnotation = mapView.dequeueReusableAnnotationView(withIdentifier: "driver") else {
let driverAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: "driver")
driverAnnotation.image = UIImage(named: "driverAnnotation")
return driverAnnotation
}
dequeuedDriverAnnotation.annotation = annotation
return dequeuedDriverAnnotation
} else if let annotation = annotation as? PassengerAnnotation {
guard let dequeuedPassengerAnnotation = mapView.dequeueReusableAnnotationView(withIdentifier: "passenger") else {
let passengerAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: "passenger")
passengerAnnotation.image = UIImage(named: "currentLocationAnnotation")
return passengerAnnotation
}
dequeuedPassengerAnnotation.annotation = annotation
return dequeuedPassengerAnnotation
}
return nil
}
}
【问题讨论】:
-
没有真正回答您的问题,但通常这是由后端 API 解决的,该 API 将接受当前位置和要覆盖的半径。所以计算可以更容易地在服务器上与数据库数据进行比较
标签: swift mkmapview mkannotationview