iOS Swift百度地圖 添加多個標註

來源:互聯網
上載者:User
搞了一天終於把添加多個標註做出來了,又深刻的反應了寫代碼一味的copy,不知其所以然的後果太嚴重,一點錯誤找個大半天,好了,不過多bb,說說添加多個標註要注意的一些東西吧。
首先,添加圖釘的方法要實現在_mapview.delegate = self 之後,否則過早添加了圖釘,並不會去調用BMKMapViewDelegate的代理方法去重新整理mapview
第二,添加圖釘方法中,BMKPointAnnotation的初始化必須放在迴圈內,否則pointAnnotation雖然會被添加三次,但這三個pointAnnotation都是同一個對象,最後繪製出的圖釘也只會有一個
//    添加圖釘    func addPointAnnotation() {        if(pointAnnotation == nil) {                        let ary1:NSArray = ["31.222771","39.915 ","31.229003"]            let ary2:NSArray = ["121.490317","116.404","121.448224"]                        var coor: CLLocationCoordinate2D = CLLocationCoordinate2D.init()                        for(var i=0;i<ary1.count;i++) {                        pointAnnotation = BMKPointAnnotation.init() //必須放在迴圈裡初始化            coor.latitude  = ary1[i].doubleValue            coor.longitude = ary2[i].doubleValue                        pointAnnotation.coordinate = coor            pointAnnotation.title = "哈嘍"                            _mapview.addAnnotation(pointAnnotation)                    }        }    }



最後,BMKMapViewDelegate繪製view

// 根據anntation產生對應的View    func mapView(mapView: BMKMapView!, viewForAnnotation annotation: BMKAnnotation!) -> BMKAnnotationView! {        //        //annotation        let annotationViewID = "renameMark"        var annotationView:BMKPinAnnotationView? = _mapview.dequeueReusableAnnotationViewWithIdentifier(annotationViewID) as? BMKPinAnnotationView                if(annotationView == nil){                        annotationView = BMKPinAnnotationView.init(annotation:annotation, reuseIdentifier:annotationViewID)        }                //設定顏色//            annotationView?.pinColor = BMKPinAnnotationColorPurple        //從天上掉下來效果        annotationView!.animatesDrop = true        //設定不可拖拽        annotationView!.draggable = false        annotationView!.image = UIImage(named:"sina")        return annotationView        }}



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.