The goal is to adjust the colors of the contacts to some values stored in the structure array.
For some help here, I implemented the following delegate method, viewForAnnotation, and works great on this delegate method in a loop based on the size of the structure's data array. Thus, it works if I want to set all contacts to the same color, for example purple (this is the commented line in the code below).
The problem is that when I set the switch to set the color based on the value in my array, it goes through this code, but doesn't take into account any of the case values to set it to an alternate color, and everything goes to the red pin ( apparently by default). I printed out the status and debugged it to find out that it enters the switch and sets pinColor accordingly, but they don't seem to stick.
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let theindex = mystructindex
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
if (theindex < MySupplierData.count) {
switch MySupplierData[mystructindex].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;
}
}
}
else {
pinView!.annotation = annotation
}
return pinView
}
Inside the for loop inside the ViewController, I call this as follows, but I do nothing with the return.
self.theMapView.addAnnotation(myAnnotation)
mapView(theMapView, viewForAnnotation: myAnnotation)
I am not doing anything with Pinview return - I didn’t think I needed it, but all the pins are painted red at this moment when using the switch code. Essentially, I have to skip something.
7-8-14 Updates to troubleshoot revised code for one great help / tutoring at Anna. Tks!
: ,
. ,
, , , .
, - , , 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
}
}
mapView - CustomMapPinAnnotation:
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView! {
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
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;
}
}
}
else {
pinView!.annotation = annotation
}
return pinView
}
viewDidLoad For
override func viewDidLoad() {
super.viewDidLoad()
var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(MySupplierData[0].latitude, MySupplierData[0].longitude), theSpan)
self.theMapView.mapType = MKMapType.Standard
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
myCustomPinAnnotation = CustomMapPinAnnotation(
coordinate: CLLocationCoordinate2DMake(MySupplierData[mystructindex].latitude,MySupplierData[mystructindex].longitude),
title: mytitle,
subtitle: mysubtitle,
status: MySupplierData[mystructindex].status)
self.theMapView.addAnnotation(myCustomPinAnnotation)
}
self.theMapView.setRegion(theRegion, animated: true)
}
, For , , myCustomPinAnnotation , viewForAnnotation mapView
. , , viewForAnnotation mapView
, , . ,
, , .