簡體   English   中英

如何使用Swift更改MKAnnotation Color?

[英]How to change MKAnnotation Color using Swift?

我在地圖上設置了MKAnnotations ,但我想更改不同場景的注釋顏色。 有沒有辦法改變注釋的顏色?

以下是我的代碼,如何實現顏色變化?

override func viewDidAppear(animated: Bool) {
    var annotationQuery = PFQuery(className: "Post")
    currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
    //annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
    annotationQuery.whereKeyExists("Location")
    annotationQuery.findObjectsInBackgroundWithBlock {
        (points, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successful query for annotations")
            // Do something with the found objects

            let myPosts = points as! [PFObject]

            for post in myPosts {
                let point = post["Location"] as! PFGeoPoint
                let annotation = MKPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                annotation.title = post["title"] as! String!
                annotation.subtitle = post["username"] as! String!


                self.mapView.addAnnotation(annotation)
            }

        } else {
            // Log details of the failure
            println("Error: \(error)")
        }
    }

您可以將自定義圖像用於注釋視圖,也可以將預定義的MKPinAnnotationViewpinColor MKPinAnnotationView使用。 但pinColors僅限於紅色,綠色和紫色。

一些例子:

import UIKit
import MapKit

class Annotation: NSObject, MKAnnotation
{
    var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
    var custom_image: Bool = true
    var color: MKPinAnnotationColor = MKPinAnnotationColor.Purple
}

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self;

    let annotation = Annotation.new()
    mapView.addAnnotation(annotation)

    let annotation2 = Annotation.new()
    annotation2.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 1.0)
    annotation2.custom_image = false
    mapView.addAnnotation(annotation2)

    let annotation3 = Annotation.new()
    annotation3.coordinate = CLLocationCoordinate2D(latitude: 1.0, longitude:  0.0)
    annotation3.custom_image = false
    annotation3.color = MKPinAnnotationColor.Green
    mapView.addAnnotation(annotation3)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        return nil
    }

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        if let anAnnotation = annotation as? Annotation {
            if anAnnotation.custom_image {
                let reuseId = "custom_image"
                anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                anView.image = UIImage(named:"custom_image")
            }
            else {
                let reuseId = "pin"
                let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                pinView.pinColor = anAnnotation.color
                anView = pinView
            }
        }
        anView.canShowCallout = false
    }
    else {
        anView.annotation = annotation
    }

    return anView
}
}

更新:在viewDidLoad中為mapView設置委托

您必須在viewForAnnotation方法中設置它。 在本教程中很好地解釋了如何做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM