亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

快速注釋不同圖像

快速注釋不同圖像

神不在的星期二 2019-11-14 10:14:19
我設法在Swift中為注解圖釘獲得了一個自定義圖標,但是現在我仍然對2個不同的注解使用不同的圖像?,F在,一個按鈕會向地圖添加注釋。應該有另一個按鈕,該按鈕也添加了注釋,但帶有另一個圖標。有沒有辦法為此使用復用ID?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}
查看完整描述

3 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

在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

}


查看完整回答
反對 回復 2019-11-14
?
繁花如伊

TA貢獻2012條經驗 獲得超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!

}


查看完整回答
反對 回復 2019-11-14
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

需要將注釋向下轉換為CustomPointAnnotation。更改為let pickupCustomAnno = annotation as! CustomPointAnnotation

查看完整回答
反對 回復 2019-11-14
  • 3 回答
  • 0 關注
  • 625 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號