在展开Optional值

时间:2018-01-08 10:55:04

标签: ios swift mkmapview overlays

我已经写了这两个函数来绘制叠加层并在viewDidLoad()处理正常。

但是,我必须通过在点击事件上传递新坐标来显示更新的叠加层。

第一个功能是在CLLocationCoordinate2D类型Array中获取位置的纬度和经度。第二个功能是绘制带有这些坐标的叠加层。

每次我在点击时调用此函数时,我面临的问题是函数MKMapView中的drawpolyline(loclist: Array<CLLocationCoordinate2D>)对象为零。

这是功能:

func drawline(locs: Array<Location>){
    var loclist: Array<CLLocationCoordinate2D> = []

    if (locs.count > 3){

        for loc in locs{
            if (loc.gps == 1){
                let cord = CLLocationCoordinate2D(latitude: loc.lat!, longitude: loc.lon!)
                loclist.append(cord)
                }
            }
        }

   drawpolyline(loclist: loclist)

}

绘制叠加层的功能是:

func drawpolyline(loclist: Array<CLLocationCoordinate2D>){
    if(loclist != nil && loclist.count > 1){

         if let overlays = self.mainMapView?.overlays {
            self.mainMapView.removeOverlays(overlays)
            }
        if let annotations = self.mainMapView?.annotations {
            self.mainMapView.removeAnnotations(annotations)
            }

        let startmark = loclist.first
        let endmark = loclist.last
        let cord1 = CLLocationCoordinate2D(latitude: (startmark?.latitude)!, longitude: (startmark?.longitude)!)

        let polyline = MKPolyline(coordinates: loclist, count: loclist.count)
        print(polyline.pointCount)

        let mapAnnotation = MKPointAnnotation();
        mapAnnotation.title = Common.SelectedDevice?.dn;
        mapAnnotation.subtitle = Common.SelectedDevice?.dn;
        mapAnnotation.coordinate = cord1
        self.iconImage = Common.getIcon((Common.SelectedDevice?.icon)!, ign:(Common.SelectedDevice?.ign!)!)
        self.addDefaultMapPin("Start" , coordinates: startmark!)
        self.addDefaultMapPin("end" , coordinates: endmark!)

            self.mainMapView.add(polyline)
            self.mainMapView.addAnnotation(mapAnnotation);
            self.zoomPolyline(polyline)


    }
    else{
        configureMap()
    }

}

0 个答案:

没有答案