【问题标题】:MapKit Overlay Swift IssueMapKit 覆盖 Swift 问题
【发布时间】:2016-01-22 11:49:28
【问题描述】:

我目前无法清除叠加层。我想清除已行进的叠加层,然后重新开始。我查看了大量不同的代码行,但似乎无法弄清楚。你能帮我解决这个问题吗?我是 Swift 的新手,所以请好好玩 :)

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var theMap: MKMapView!
    @IBOutlet weak var theLabel: UILabel!
    @IBOutlet weak var startTracking: UIButton!
    @IBOutlet weak var stopTracking: UIButton!
    @IBOutlet weak var clearTrack: UIButton!

    var polyline:MKPolyline = MKPolyline()
    var manager:CLLocationManager!
    var myLocations: [CLLocation] = []

    func clearTrack(sender: UIButton){
        //label.text = String(myLocations.count)
        //stopTracking()
       theMap.removeOverlay (polyline)  
    }

    func stopTracking(sender: UIButton) {
        manager.stopUpdatingLocation()
        myLocations = []
        println("Stop making a line")
    }

    func startTracking(sender: UIButton) {
        manager.startUpdatingLocation()
        myLocations = []
        println("Making a line")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        //Setup our Location Manager
        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()

        //Setup our Map View
        theMap.delegate = self
        theMap.mapType = MKMapType.Hybrid
        theMap.showsUserLocation = true
    }

    func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
        theLabel.text = "\(locations[0])"
        myLocations.append(locations[0] as CLLocation)

        let spanX = 0.002
        let spanY = 0.002
        var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
        theMap.setRegion(newRegion, animated: true)

        if (myLocations.count > 1){
            var sourceIndex = myLocations.count - 1
            var destinationIndex = myLocations.count - 2

            let c1 = myLocations[sourceIndex].coordinate
            let c2 = myLocations[destinationIndex].coordinate
            var a = [c1, c2]
            var polyline = MKPolyline(coordinates: &a, count: a.count)
            theMap.addOverlay(polyline)
        }
    }

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

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

【问题讨论】:

  • 你尝试了什么?了解这一点对我们很重要,否则我们可能会向您展示您已经尝试过的代码。
  • theMap.removeOverlay (polyline)clearTrack 内部不起作用的部分原因是它指的是类级别 变量polyline 但在didUpdateLocations 中,叠加层是使用 local 变量 (var polyline = ...) 添加的,该变量恰好具有相同的名称,但两者完全不同。
  • 我已经尝试过 IBAction 函数。我尝试删除 addOverlay 并将其作为 startTracking 选项的一部分。我还没有尝试过戴夫在下面所说的,我回家后会尝试。

标签: ios swift mapkit mkoverlay


【解决方案1】:

您是否尝试过使用:

func clearTrack(sender: UIButton){
    theMap.removeOverlays(theMap.overlays)
}

【讨论】:

  • 戴夫 非常感谢您的出色工作。看了你的回答,感觉还不错。我尝试了一些不同的东西,只是不是你说的哈哈,再次感谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多