注释贴图视图中的图钉颜色不会更改

时间:2015-11-26 06:31:18

标签: ios swift mapkit mkannotation mkannotationview

我想将MapView上绘制的注释的引脚颜色更改为红色以外的颜色(默认)。但我无法这样做。以下是我到目前为止所尝试的内容。所有我看到的都是红色,下面是代码。

 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;
        }
    }

新代码:

//*************************************************************//
//                                                             //
//     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
        }
    }
}

准备另一个视图控制器

 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)
    }

1 个答案:

答案 0 :(得分:2)

添加注释的过程是:

let annotation = MKPointAnnotation()
annotation.coordinate = ...
annotation.title = ...

mapView.addAnnotation(annotation)

但是,在系统要求之前,您不会对注释视图执行任何操作。因此,将视图控制器设置为地图视图的委托,然后实现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
}

请参阅位置和地图编程指南中的Adding Annotations to Map