Ios 地图注释有很多问题
对于一段非常简单的代码,我有几个问题。目标是从数据库中读取项目,并将其固定在地图中。如果项目已标记为收藏夹,则pin应为其他颜色 第一个问题是并不是所有的项目都被渲染。在我将使用的示例中,查询返回了12个结果,我已经验证了每个项都创建了一个MKAnnotation,并且每个注释都调用了ViewFor 除了没有显示所有管脚之外,还有两个问题 滚动地图时,第一个PIN会随机丢失其标题 其次,最喜欢的颜色(绿色色调)很少呈现绿色。80%的情况下,它以标准蓝色显示。我再次验证了mkMarkerNotationView颜色设置是否正确 面对所有这些问题,我不得不得出结论,我在做一些根本上非常错误的事情。这很奇怪,因为这看起来非常简单Ios 地图注释有很多问题,ios,swift,mkmapview,mkannotationview,Ios,Swift,Mkmapview,Mkannotationview,对于一段非常简单的代码,我有几个问题。目标是从数据库中读取项目,并将其固定在地图中。如果项目已标记为收藏夹,则pin应为其他颜色 第一个问题是并不是所有的项目都被渲染。在我将使用的示例中,查询返回了12个结果,我已经验证了每个项都创建了一个MKAnnotation,并且每个注释都调用了ViewFor 除了没有显示所有管脚之外,还有两个问题 滚动地图时,第一个PIN会随机丢失其标题 其次,最喜欢的颜色(绿色色调)很少呈现绿色。80%的情况下,它以标准蓝色显示。我再次验证了mkMarkerNotat
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
}
类MapViewController:UIViewController、CLLocationManagerDelegate、MKMapViewDelegate{
@ibvar映射视图:MKMapView!
重写func viewDidLoad(){
super.viewDidLoad()
mapView.delegate=self
mapView.showAnnotations(mapView.annotations,动画:true)
//为地图上现在可见的所有设施创建注释
func映射视图(uMapView:MKMapView,RegiondChangeAnimated:Bool){
设edgePoints=mapView.edgePoints()
设minLat=edgePoints.ne.latitudeedgePoints.sw.latitude?edgePoints.ne.latitude:edgePoints.sw.latitude
设minLong=edgePoints.ne.longitudeedgePoints.sw.longitude?edgePoints.ne.longitude:edgePoints.sw.longitude
让可见的城市重新开始=
managedObjectModel.fetchRequestFromTemplate(名称为:“FetchByCoordinates”,替换变量:[“minLat”:minLat,“minLong”:minLong,“maxLat”:maxLat,“maxLong”:maxLong])
做{
让facilities=try CoreDataHelper.shared.persistentContainer.viewContext.fetch(VisibleCitiesRequest!)作为![FacilityMO]
设施中的设施{
让facilityMarker=facilityMarker(名称:facility.facilityName!、地址:facility.addressLine1!、位置:facility.location!、电话:facility.phoneNumber!、提供商编号:facility.providerNumber!、收藏夹:facility.facility)
mapView.addAnnotation(facilityMarker)
}
}抓住{
让alertController=UIAlertController(标题:“无设施”,消息:“可见地图区域内无设施”,首选样式:。警报)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.cancel,处理程序:nil))
self.present(alertController,动画:true,完成:nil)
}
}
//把大头针放在地图上
func mapView(uMapView:MKMapView,viewFor annotation:MKAnnotation)->MKAnnotationView{
guard let annotation=注释为?FacilityMarker else{return nil}
let identifier=“设施”
变量视图:mkmarkerNotationView
如果让dequeuedView=mapView.dequeueReusableAnnotationView(带标识符:标识符)作为?
mkMarkerNotationView{
dequeuedView.annotation=注释
view=dequeuedView
}否则{
打印(“为:\(annotation.title!)创建新视图”)
view=mkmarkerNotationView(注释:注释,reuseIdentifier:identifier)
}
//设置颜色
if(annotation.favorite){
view.markerTintColor=.green
view.tintColor=.green
}否则{
view.markertincolor=.blue
view.tintColor=.blue
}
返回视图
}
这是查询返回的实际数据。仅呈现突出显示的项。
这在所有测试中都是100%一致的
- 创建新视图:SEMINOLE透析中心
- 创建新视图:塞米诺尔湖
- 创建新视图:RAI护理中心-拉戈
- 创建新视图:BAY BREEZE透析诊所有限公司
- 创建新视图:CLEARWATER有限责任公司RENVIVA透析中心
- 创建新视图:FKC-BELLEAIR透析中心
- 创建新视图:RAI护理中心-清水
- 创造新的视角:FMC-贝莱尔家庭疗法
- 创建新视图:CORVA海湾海岸透析中心
- 创建新视图:GULF BREEZE透析中心
- 创建新视图:BMA-CLEARWATER
- 创建新视图:RAI-US 19 NORTH-CLEARWATER
- 拉戈是蓝色的
- BMA-清水是绿色的
- CLEARWATER有限责任公司的RENVIVA透析中心是蓝色的
- RAI护理中心-清水是蓝色的
- FKC-BELLEAIR透析中心为蓝色
- FMC-贝莱尔家庭疗法是蓝色的
- 海湾微风透析中心是蓝色的
- 海湾微风透析诊所公司是蓝色的
- RAI-US 19北清水是蓝色的
- 科瓦湾沿岸透析中心是蓝色的
...
// 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
}
...
Ogawa san我以前也打印过类似的东西,但我去掉了大部分调试语句以减少混乱