注释地图视图中的图钉颜色没有改变

问题描述 投票:0回答:2

我想将 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
        }
    }
}

准备在另一个视图控制器上进行 SEGUE

 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)
    }
ios swift mapkit mkannotation mkannotationview
2个回答
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
}

请参阅位置和地图编程指南中的向地图添加注释


0
投票

从 iOS 16 开始:MKPinAnnotationView 已弃用。使用 MKMarkerAnnotationView 代替

稍微修改一下上面的答案:

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? MKMarkerAnnotationView
    if annotationView == nil {
        annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.markerTintColor = UIColor.greenColor()
        annotationView?.canShowCallout = true
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}
© www.soinside.com 2019 - 2024. All rights reserved.