gpt4 book ai didi

ios - 如何使用 Swift 更改 MKAnnotation 颜色?

转载 作者:可可西里 更新时间:2023-11-01 00:16:38 25 4
gpt4 key购买 nike

我在 map 上设置了 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)")
}
}

最佳答案

您可以将自定义图像用于注释 View 或使用预定义的 MKPinAnnotationViewpinColor。但 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 设置委托(delegate)

关于ios - 如何使用 Swift 更改 MKAnnotation 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29842013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com