【问题标题】:Polyline Overlay in SwiftSwift 中的折线叠加
【发布时间】:2019-01-09 15:17:20
【问题描述】:

我有我的 MKMapViewDelegate。另外,MapView.delegate = self

let c1 = myCLLocationCoodinate
let c2 = myCLLocationCoodinate2
var a = [c1, c2]
var polyline = MKPolyline(coordinates: &a, count: a.count)
self.MapView.addOverlay(polyline)

使用此委托方法:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

    if overlay is MKPolyline {
        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.whiteColor()
        polylineRenderer.lineWidth = 2 
        return polylineRenderer
    }
    return nil
}

我明白了:EXC BAD ACCESS Thread 8 on

self.MapView.addOverlay(polyline)

【问题讨论】:

    标签: ios swift mkmapview mkpolyline


    【解决方案1】:

    我认为这里的问题在于这条线:

    var a = [c1, c2]
    

    这里你直接创建了数组,没有指定它的类型。

    参见下面的参考代码来创建折线覆盖和相关的委托方法:

    let c1 = myCLLocationCoodinate
    let c2 = myCLLocationCoodinate2
    
    var points: [CLLocationCoordinate2D]
    points = [c1, c2]
    
    var geodesic = MKGeodesicPolyline(coordinates: &points[0], count: 2)
    mapView.add(geodesic)
    
    UIView.animate(withDuration: 1.5, animations: { () -> Void in
        let span = MKCoordinateSpanMake(20, 20)
        let region1 = MKCoordinateRegion(center: c1, span: span)
        mapView.setRegion(region1, animated: true)
    })
    

    渲染叠加层的委托方法:

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    
       if overlay is MKPolyline {
           var polylineRenderer = MKPolylineRenderer(overlay: overlay)
           polylineRenderer.strokeColor = UIColor.whiteColor()
           polylineRenderer.lineWidth = 2 
           return polylineRenderer
       } 
       return nil
    }
    

    【讨论】:

      【解决方案2】:

      您的地图视图似乎已被释放。折线构造没问题。

      通常,变量以小写字母开头。您是否对地图视图进行了子类化并尝试访问该类?

      【讨论】:

      • 我不确定这是否正确。如果MapView 被释放,什么都不会发生。
      • 我在这里声明:@IBOutlet var MapView: MKMapView!是的名字应该更好。 :)
      • 我在委托方法上打了个断点,它一步一步通过它没问题。
      【解决方案3】:

      我在这上面花了太多时间 WAAAAAAYYYY,所以我想我会为类似的问题添加解决方案。我在带有 MKPolygon 的 addOverlay 上获得了 EXC BAD ACCESS。原来我一直都在错误的线程上。修复它:

      var points = [MKMapPoint]()
      for var i = 0; i < area.coordinates.count; i+=2 {
        let c = CLLocationCoordinate2DMake(area.coordinates[i], area.coordinates[i+1])
        points.append(MKMapPointForCoordinate(c))
      }
      let polygon = MKPolygon(points: &points, count: points.count)
      dispatch_async(dispatch_get_main_queue(), {
        self.mapView.addOverlay(polygon)
      })
      

      【讨论】:

        【解决方案4】:
        let firstlat : string = "12.9166"
        let firstlon : string = "77.6101"
        
        let secondlat : string = "12.9610"
        let secondLon : string = "77.6387"
        
            let point1 = CLLocationCoordinate2DMake(Double(firstlat)!, Double(firstlon)!)
            let point2 = CLLocationCoordinate2DMake(Double(secondlat as String)!, Double(secondLon)!)
        
            let pickAnnotation : MKPointAnnotation = MKPointAnnotation()
            pickAnnotation.coordinate = point1
            pickAnnotation.title = "pick"
            displayMapView.addAnnotation(pickAnnotation)
        
            let dropAnnotation : MKPointAnnotation = MKPointAnnotation()
            dropAnnotation.coordinate = point2
            dropAnnotation.title = "drop"
            displayMapView.addAnnotation(dropAnnotation)
            displayMapView.showAnnotations(displayMapView.annotations, animated: true)
        
            var points: [CLLocationCoordinate2D]
            points = [point1, point2]
           routeLine = MKPolyline(coordinates: &points[0] , count: 2)
            displayMapView.add(routeLine)
        

        【讨论】:

          【解决方案5】:
              func showRouteOnMap(_ pickCoordinate: CLLocationCoordinate2D, _ destinationCoordinate: CLLocationCoordinate2D) {
          
              let request = MKDirections.Request()
          
              let sourcePlacemark = MKPlacemark(coordinate: pickCoordinate)
              let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
          
              request.source = sourceMapItem
          
              let myPlacemark = MKPlacemark(coordinate: destinationCoordinate)
              let destinationMapItem = MKMapItem(placemark: myPlacemark)
              request.destination = destinationMapItem
          
              request.requestsAlternateRoutes = false
          
              let directions = MKDirections(request: request)
          
              directions.calculate(completionHandler: {(response, error) in
          
                  if let error = error {
                      print(error.localizedDescription)
                  } else {
                      if let response = response {
                          self.showRoute(response)
                      }
                  }
              })
          
          }
          
          func showRoute(_ response: MKDirections.Response) {
          
              for route in response.routes {
                  routeMap.addOverlay(route.polyline,
                               level: MKOverlayLevel.aboveRoads)
                  self.routeMap.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
              }
          
          }
          
          // MARK: - MKMapViewDelegate
          

          func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

              let renderer = MKPolylineRenderer(overlay: overlay)
          
              renderer.strokeColor = UIColor(red: 17.0/255.0, green: 147.0/255.0, blue: 255.0/255.0, alpha: 1)
          
              renderer.lineWidth = 5.0
          
              return renderer
          }
          

          【讨论】:

          • 请将您的答案放在上下文中并用简短的句子进行解释,而不是仅仅粘贴代码(而且格式不正确)
          猜你喜欢
          • 2017-03-10
          • 2017-03-23
          • 1970-01-01
          • 2018-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-02
          相关资源
          最近更新 更多