I want to change the pin color of annotations drawn on MapView to a color other than Red( Default). But I am unable to do so. Below is what I have tried till now. All I see in red colour with he below code.
override func viewDidLoad()
{
for coordinateItem in arrayOfPFObject
{
print(coordinateItem)
self.pointAnnotation = MKPointAnnotation()
self.lat_ = coordinateItem["Latitude"] as! Double
self.long_ = coordinateItem["Longitude"] as! Double
self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: self.lat_, longitude: self.long_)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.dispAnnotation.centerCoordinate = self.pointAnnotation.coordinate
self.dispAnnotation.addAnnotation(self.pinAnnotationView.annotation!)
self.pointAnnotation.title = (coordinateItem["Address"] as! String) + " " + (coordinateItem["FirstName"] as! String)
self.pinAnnotationView.pinColor = MKPinAnnotationColor.Green
self.pinAnnotationView.pinTintColor = MKPinAnnotationView.greenPinColor()
self.pinAnnotationView.pinColor = MKPinAnnotationColor.Purple
self.pinAnnotationView.canShowCallout = true
//.pinView.canShowCallout = YES;
}
}
NEW CODE:
//*************************************************************//
// //
// File Name: MultipleAnnotationViewController.swift //
// App Name: LifeLine //
// Created by ************ on 11/23/15. //
// Copyright © 2015 *************. All rights reserved. //
// Course Name: ***************************** //
// CourseCode: ******************* //
// Language: Swift 2.1 //
// Tools: Xcode Version 7.0.1 //
// DevelopedOn: OS X Yosemite 10.10.5 //
// Device Suport: IPhones //
// //
//*************************************************************//
/*
* File to show the annotations for matching Blood Type donor in the MapView
*
*/
import UIKit
import MapKit
class MultipleAnnotationViewController: UIViewController
{
var searchController:UISearchController! // Manages the presentation of searchbar
var annotation:MKAnnotation! // Reference to drawn annotation
var localSearchRequest:MKLocalSearchRequest! // This object is prepared and passed to MKLocalSearch object
var localSearch:MKLocalSearch! // It will initiate the search of location entered asynchronously
var localSearchResponse:MKLocalSearchResponse! // Search Response is stored in this var
var error:NSError! // Error if any
var pointAnnotation:MKPointAnnotation! // Work for drawing annotation pin once location found
var pinAnnotationView:MKPinAnnotationView!
var coordinateArray: [CLLocationCoordinate2D]? //Longitude and latitude array
var arrayOfPFObject: [PFObject] = [PFObject]()
var lat_ :Double = 0
var long_ :Double = 0
@IBOutlet weak var dispAnnotation: MKMapView! // Outlet for displaying multiple annotations
override func viewDidLoad()
{
super.viewDidLoad()
dispAnnotation.delegate = self // ERROR : Cannot assign a value of type 'MultipleAnnotationViewController' to a value of type 'MKMapViewDelegate?'
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
for coordinateItem in arrayOfPFObject
{
print(coordinateItem)
self.pointAnnotation = MKPointAnnotation()// a+
self.lat_ = coordinateItem["Latitude"] as! Double
self.long_ = coordinateItem["Longitude"] as! Double
self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: self.lat_, longitude: self.long_)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.dispAnnotation.centerCoordinate = self.pointAnnotation.coordinate
self.dispAnnotation.addAnnotation(self.pinAnnotationView.annotation!)
self.pointAnnotation.title = (coordinateItem["Address"] as! String) + " " + (coordinateItem["FirstName"] as! String)
pinAnnotationView?.pinTintColor = UIColor.greenColor()
self.pinAnnotationView.canShowCallout = true
}
}
}
PREPARE FOR SEGUE ON ANOTHER VIEW CONTROLLER
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
lat_ = Double((location?.coordinate.latitude)!)
long_ = Double((location?.coordinate.longitude)!)
print(long_)
let query = PFQuery(className: "_User")
query.whereKey("BloodGroup", equalTo: bloodType.text!)
query.whereKey("Latitude", lessThan: lat_ + 0.60)
query.whereKey("Latitude", greaterThan: lat_ - 0.60)
// print(lat_)
query.whereKey("Longitude", greaterThan: long_ - 0.60)
query.whereKey("Longitude", lessThan: long_ + 0.60)
do
{
try arrayOfPFObject = query.findObjects() as [PFObject]
}
catch
{
// TODO: Exception Handling
}
let dest: MultipleAnnotationViewController = segue.destinationViewController as! MultipleAnnotationViewController
dest.arrayOfPFObject = arrayOfPFObject
print(arrayOfPFObject)
}
The process for adding the annotation is :
let annotation = MKPointAnnotation()
annotation.coordinate = ...
annotation.title = ...
mapView.addAnnotation(annotation)
But you don't do anything with the annotation view until the system asks for it. So set your view controller as the map view's delegate, and then implement viewForAnnotation
:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
let identifier = "com.domain.app.something"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.pinTintColor = UIColor.greenColor()
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
return annotationView
}
See Adding Annotations to Map in the Location and Maps Programming Guide.