如何使用swift在mapKit中的两个不同标记上放置两个图像

时间:2016-01-29 04:40:21

标签: swift2 mkmapview mapkit mkannotation mkannotationview

我想在我的ignitionon.png中更改标记图像并且我已成功更改了我的图像,但问题是我有2个标记点,我想将ignitionof.png放在第1点和{{1}在第二点和我的逻辑位置ignitionon.png在两个点上。

代码

if(self.doubelLocation) {
            var pointOff =  CLLocationCoordinate2D()
            var pointOn  = CLLocationCoordinate2D()
            if(self.dateTime.count > 0) {
                for(var i : Int = 0 ; i < self.dateTime.count ; i++) {
                    // print("Real status = \(self.dateTime[i])")
                    var fixTime = self.dateTime[i]
                    if(fixTime == self.dateTimeTwo) {
                        pointOff = CLLocationCoordinate2DMake(self.latt[i], self.lngg[i]);
                        print("pointOff = \(pointOff)")
                        //points.append(pointOf)
                    }
                    if(fixTime == self.dateTimeOne){

                        pointOn = CLLocationCoordinate2DMake(self.latt[i], self.lngg[i]);

                        print("pointOn = \(pointOn)")
                        //points.append(pointOf)

                    }

                }
                var points: [CLLocationCoordinate2D]

                points = [pointOn, pointOff]

                dispatch_async(dispatch_get_main_queue(), {
                    let geodesic = MKGeodesicPolyline(coordinates: &points[0], count: 2)
                    self.theMapView.addOverlay(geodesic)
                    let latDelta:CLLocationDegrees = 0.03
                    let lnggDelta:CLLocationDegrees = 0.03

                    UIView.animateWithDuration(1.5, animations: { () -> Void in
                        let span = MKCoordinateSpanMake(latDelta, lnggDelta)
                        let region1 = MKCoordinateRegion(center: points[0], span: span)
                        self.theMapView.setRegion(region1, animated: true)
                        self.ActivityIndicator.stopAnimating()


                        for(var i : Int = 0 ;i < points.count; i++){

                            var st = self.reportText[i]



                         //   let theMarker = MKPointAnnotation()
                            //how to change marker color
                            //https://stackoverflow.com/questions/33532883/add-different-pin-color-with-mapkit-in-swift-2-1

                        let theMarker = MKPointAnnotation()
                            theMarker.coordinate = points[i]



                          //  if(st == "IGNITION ON"){
                            if(i == 0){

                                theMarker.title = "Status : IGNITION OFF"
                                theMarker.subtitle = "\(self.locationOff)"
                                // theMarker.subtitle = "Date = , Reg#:  "
                                self.theMapView.addAnnotation(theMarker)

                                let anView1:MKAnnotationView = MKAnnotationView()
                                anView1.annotation = theMarker
                                anView1.image = UIImage(named:"ignitionof")
                                anView1.canShowCallout = true
                                anView1.enabled = true
                            }
                            if(i == 1){

                              //  theMarker = UIColor.greenColor()
                                theMarker.title = "Status : IGNITION ON"
                                theMarker.subtitle = "\(self.locationOn)"

                                // theMarker.subtitle = "Date = , Reg#:  "
                                self.theMapView.addAnnotation(theMarker)

                                //how to change image of marker 
                                //https://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview

                                let anView:MKAnnotationView = MKAnnotationView()
                                anView.annotation = theMarker
                                anView.image = UIImage(named:"ignitionon")
                                anView.canShowCallout = true
                                anView.enabled = true
                            }
                         //   }



                        }
                    })

                })

            }


        }

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.image = UIImage(named:"ignitionon")
        anView!.canShowCallout = true
    }
    var anView1 = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView1 == nil {
        anView1 = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView1!.image = UIImage(named:"ignitionof")
        anView1!.canShowCallout = true
    }
    else {
        //we are re-using a view, update its annotation reference...
        anView!.annotation = annotation
    }

    return anView
}

我正在关注此链接: Swift - Add MKAnnotationView To MKMapView

1 个答案:

答案 0 :(得分:0)

这不是处理具有不同元数据的多个标记的最佳方式。 您不能使用mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)两次或更多次,因为在viewForAnnotation中,您在堆栈中添加的每个Point只有1个视图。

创建一个MKPointAnnotation的子类:

class CustomPointAnnotation: MKPointAnnotation {

    var tag: String!

}

创建包含所有图像的词典:

var imagesPath : ["tag_1": "image_1.png", "tag_2": "image_2.jpg"]

现在在委托函数中,选中Simply

if !(annotation is CustomPointAnnotation) {
            return nil
        }

并处理您拥有的唯一一个视图:

let reuseId = "test"

var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
    anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
   var customannotation = annotation as! CustomPointAnnotation
        anView!.image = UIImage(named: imagesPath(customannotation.tag))
    anView!.canShowCallout = true
}

添加新自定义点的示例是:

let aPoint = CustomPointAnnotation()
    aPoint.coordinate = CLLocationCoordinate2DMake(40.730872, -73.003066)
    aPoint.title = "Info1"
    aPoint.subtitle = "Subtitle"
    aPoint.tag = "tag_1"

    mapView.addAnnotation(aPoint)