Kok*_*nee 5 mkannotationview ios swift
目标是根据存储在结构数组中的某些值自定义引脚颜色.
根据这里的一些帮助,我实现了以下viewForAnnotation委托方法,并且可以在基于结构数据数组大小的循环中迭代地调用此委托方法.因此,如果我想将所有引脚设置为一种颜色,例如紫色(这是下面代码中的注释行),它就可以工作.
问题是,当我根据数组中的值设置颜色时,它会通过此代码,但不会考虑任何案例值将其设置为备用颜色,并且所有内容都转到红色引脚(看似默认).我打印出状态并进行了调试,知道它已经进入开关并相应地设置了pinColor,但它们似乎并没有坚持下去.
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let theindex = mystructindex // grab the index from a global to be used below
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
// Preventive if to keep this from being called beyond my arrays index value as the delegate getting called beyond the for loop for some unknown reason
if (theindex < MySupplierData.count) {
// Set the pin color based on the status value in MySupplierData structure array
switch MySupplierData[mystructindex].status {
case 0,1:
println("Case 0 or 1 - setting to Red")
pinView!.pinColor = .Red // Needs help, show red pin
case 2:
println("Case 2 - Setting to Green")
pinView!.pinColor = .Green // Looking Good
case 3:
println("Case 3 - Setting to Purple")
pinView!.pinColor = .Purple // Could use a follow-up
default:
println("Case default - Should Never Happen")
break;
} // end switch
} // end if
// pinView!.pinColor = .Purple // This works fine without the switch and respects any color I set it to.
}
else {
pinView!.annotation = annotation
}
return pinView
}
Run Code Online (Sandbox Code Playgroud)
在ViewController中我的for循环内部,我调用如下,但是我没有对返回做任何事情.
// previous to this I setup some Titles and Subtitle which work fine
self.theMapView.addAnnotation(myAnnotation)
// Call to my mapview
mapView(theMapView, viewForAnnotation: myAnnotation)
Run Code Online (Sandbox Code Playgroud)
我没有对返回Pinview做任何事情 - 我认为不需要,但是当使用开关代码时,此时所有引脚都被拉红.从根本上说,我必须在这里遗漏一些东西.
7-8-14每个安娜的帮助/辅导解决修改代码问题的更新.TKS!
它几乎可以工作,Map中的所有引脚都有正确的颜色,但立即显示之外的引脚有时是错误的.发布此处涉及的所有代码,因为它可能对其他人有帮助,因为这似乎是关于如何在地图中进行自定义工作的一个非常常见的问题.
建议用于在自定义注释中保存其他变量的自定义类 - 在这种情况下,状态值来自我的数据结构MySupplierData.
class CustomMapPinAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String
var subtitle: String
var status: Int
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, status: Int) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.status = status
}
}
Run Code Online (Sandbox Code Playgroud)
修改后的mapView - 现在利用传递给它的新CustomMapPinAnnotation:
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView! {
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
// Code to catch my custom CustomMapPinAnnotation so we can check the status and set the color
if annotation.isKindOfClass(CustomMapPinAnnotation)
{
println("FOUND OUR CustomMapPinAnnotation CLASS IN mapView")
println(" Custom Title = \(annotation.title)")
println(" Custom status passed = \(annotation.status)")
switch annotation.status {
case 0,1:
println("Case 0 or 1 - Setting to Red")
pinView!.pinColor = .Red
case 2:
println("Case 2 - Setting to Green")
pinView!.pinColor = .Green
case 3:
println("Case 3 - Setting to Purple")
pinView!.pinColor = .Purple
default:
println("Case default - Should Never Happen")
break;
} // switch
} // if
}
else {
pinView!.annotation = annotation
}
return pinView
} //func mapView
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad中设置和For循环来设置注释
override func viewDidLoad() {
super.viewDidLoad()
// setup the region and Span
var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
// Set the region to the the first element of the structure array.
var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(MySupplierData[0].latitude, MySupplierData[0].longitude), theSpan)
// This set the Map Type (Standard, Satellite, Hybrid)
self.theMapView.mapType = MKMapType.Standard
// Now loop through the structure data from 1 top the end of the structure to map the data
var mytitle: String = ""
var mysubtitle: String = ""
var myCustomPinAnnotation: CustomMapPinAnnotation
for mystructindex = 0; mystructindex < MySupplierData.count; ++mystructindex {
println("INSIDE SUPPLIER LOOP INDEX = \(mystructindex)" )
switch MySupplierData[mystructindex].status {
case 0:
mytitle = "(Red) " + MySupplierData[mystructindex].company
case 1:
mytitle = "(Red) " + MySupplierData[mystructindex].company
case 2:
mytitle = "(Geeen) " + MySupplierData[mystructindex].company
case 3:
mytitle = "(Purple) " + MySupplierData[mystructindex].company
default:
mytitle = "? " + MySupplierData[mystructindex].company
}
mysubtitle = MySupplierData[mystructindex].subtitle
// Create the Custom Annotations with my added status code
myCustomPinAnnotation = CustomMapPinAnnotation(
coordinate: CLLocationCoordinate2DMake(MySupplierData[mystructindex].latitude,MySupplierData[mystructindex].longitude),
title: mytitle, // custom title
subtitle: mysubtitle, // custom subtitle
status: MySupplierData[mystructindex].status) // status that will drive pin color
// put this annotation in the view.
self.theMapView.addAnnotation(myCustomPinAnnotation)
} // For
// This line brings up the display with the specific region in mind, otherwise it seems to default to a US Map.
self.theMapView.setRegion(theRegion, animated: true)
} // viewDidLoad
Run Code Online (Sandbox Code Playgroud)
调试输出显示For循环执行完成按预期完成创建myCustomPinAnnotation之前,mapView中的自定义viewForAnnotation在内部执行.当我将地图移动到直接视图之外的区域时,我注意到mapView中的viewForAnnotation会根据需要进行调用,我会看到我的开关相应地执行,但是引脚颜色并不总是正确的.初始显示图中的所有引脚每次都是正确的,所以这些外部区域我目前仍然坚持它们关闭的原因.
小智 9
首先,代码不应该viewForAnnotation自己显式调用.
删除行viewForAnnotation后的显式调用addAnnotation.
viewForAnnotation是一个委托方法,地图视图会在需要显示注释时自动调用它.如果没有自动调用,请确保设置了地图视图的delegate属性(self例如).
第二个(也就是真正的问题)是,代码假定viewForAnnotation委托方法只会在添加每个注释后立即调用一次.
情况并非如此,并不能保证.地图视图将viewForAnnotation在需要显示注释时调用,并且可以针对相同注释多次调用,或者在实际添加注释之后很长时间调用(例如,在用户平移或缩放地图并且注释进入视图之后).
请参阅MKAnnotationView缓冲其输入队列?有关其他答案的一些其他详细信息和相关链接,包括示例代码.
基本上,您必须使用注释对象本身存储影响注释视图的属性,并从annotation传入的参数中检索这些属性viewForAnnotation.
我建议您使用的是:
我假设您正在使用内置注释类MKPointAnnotation.而不是使用MKPointAnnotation哪个不允许您将自定义status属性与注释对象本身一起存储,或者:
创建一个实现MKAnnotation协议但也包含status属性的自定义类.在创建注释时设置此属性,并从annotation传入的参数中提取其值viewForAnnotation并相应地进行设置pinColor.请参阅链接答案中的示例代码.
在MySupplierData 自己的对象中创建实现MKAnnotation协议的对象.因此,如果对象MySupplierData是某个类的实例,比如说,Supplier使Supplier类符合MKAnnotation协议,那么您可以MySupplierData在调用时将对象本身添加到地图视图中addAnnotation.
| 归档时间: |
|
| 查看次数: |
7601 次 |
| 最近记录: |