快速添加不同的图像进行注释

问题描述 投票:30回答:3

我设法在Swift中为注释图钉获得了一个自定义图标,但现在我仍然对2个不同的注释使用不同的图像。现在,一个按钮会向地图添加注释。应该有另一个按钮,该按钮也添加了注释,但带有另一个图标。

是否有办法为此使用复用标识?

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var Map: MKMapView!

@IBAction func btpressed(sender: AnyObject) {

    var lat:CLLocationDegrees = 40.748708
    var long:CLLocationDegrees = -73.985643
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    Map.setRegion(region, animated: true)


    var information = MKPointAnnotation()
    information.coordinate = location
    information.title = "Test Title!"
    information.subtitle = "Subtitle"

    Map.addAnnotation(information)
}

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

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"1.png")
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }

    return anView
}
ios swift mkmapview mkannotationview mkpointannotation
3个回答
81
投票

viewForAnnotation委托方法中,根据要调用的方法image设置annotation

请确保执行此操作[,使视图出列或创建视图(不仅在if anView == nil部分中)。否则,使用出队视图的注释将显示以前使用该视图的注释的图像。

使用基本的MKPointAnnotation,区分注释的一种粗略方法是通过它们的title,但这不是很灵活。

一种更好的方法是使用实​​现MKAnnotation协议的自定义注释类(一种简单的方法是将MKPointAnnotation子类化,并添加帮助实现自定义逻辑所需的任何属性。

在自定义类中,添加一个属性,例如imageName,可用于基于注释自定义图像。

此示例子类为MKPointAnnotation

class CustomPointAnnotation: MKPointAnnotation { var imageName: String! }

创建类型为CustomPointAnnotation的注释并设置其imageName

var info1 = CustomPointAnnotation() info1.coordinate = CLLocationCoordinate2DMake(42, -84) info1.title = "Info1" info1.subtitle = "Subtitle" info1.imageName = "1.png" var info2 = CustomPointAnnotation() info2.coordinate = CLLocationCoordinate2DMake(32, -95) info2.title = "Info2" info2.subtitle = "Subtitle" info2.imageName = "2.png"

viewForAnnotation中,使用imageName属性设置视图的image

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if !(annotation is CustomPointAnnotation) { return nil } let reuseId = "test" var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) if anView == nil { anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) anView.canShowCallout = true } else { anView.annotation = annotation } //Set annotation-specific properties **AFTER** //the view is dequeued or created... let cpa = annotation as CustomPointAnnotation anView.image = UIImage(named:cpa.imageName) return anView }


12
投票
借助Anna和Fabian Boulegue的iOS Swift代码:

import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet weak var mapView: MKMapView! override func viewDidLoad() { super.viewDidLoad() self.mapView.delegate = self var info1 = CustomPointAnnotation() info1.coordinate = CLLocationCoordinate2DMake(26.889281, 75.836042) info1.title = "Info1" info1.subtitle = "Subtitle" info1.imageName = "flag.png" var info2 = CustomPointAnnotation() info2.coordinate = CLLocationCoordinate2DMake(26.862280, 75.815098) info2.title = "Info2" info2.subtitle = "Subtitle" info2.imageName = "flag.png" mapView.addAnnotation(info1) mapView.addAnnotation(info2) } func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { println("delegate called") if !(annotation is CustomPointAnnotation) { return nil } let reuseId = "test" var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) if anView == nil { anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) anView.canShowCallout = true } else { anView.annotation = annotation } //Set annotation-specific properties **AFTER** //the view is dequeued or created... let cpa = annotation as CustomPointAnnotation anView.image = UIImage(named:cpa.imageName) return anView } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } class CustomPointAnnotation: MKPointAnnotation { var imageName: String! }


0
投票

SWIFT 5

let pointAnnotation = MKPointAnnotation() override func viewDidLoad() { mapVw.delegate = self pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: yourLatitude, longitude:yourLongitude) mapVw.addAnnotation(pointAnnotation) } // MARK:- MapView Delegate func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil; }else{ let pinIdent = "Pin"; var pinView: MKAnnotationView? if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: pinIdent) { dequeuedView.annotation = annotation; pinView = dequeuedView; } else { pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdent); pinView?.image = UIImage(named: "yourImage") } return pinView; } }
© www.soinside.com 2019 - 2024. All rights reserved.