ios - Xcode Swift 删除 MKPolyline

标签 ios xcode swift mkmapview mkoverlay

我正在编写一个应用程序,它能够跟踪用户的位置并在 map 上追踪它。有三个按钮,一个是开始追踪用户放置的路径,一个是停止追踪用户放置的路径,最后一个是清除 map 上的追踪线。我在设置清除按钮时遇到问题。有人可以帮我设置@IBAction 的清除路径按钮吗?

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var theMap: MKMapView!
    @IBOutlet weak var startStopTracking: UISegmentedControl!
    @IBOutlet weak var clearPath: UIButton!
    @IBOutlet weak var label: UILabel!

    var manager:CLLocationManager!
    var myLocations: [CLLocation] = []

    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

        //Setup startStopTracking button?

        startStopTracking.selectedSegmentIndex = -1
    }

    @IBAction func indexChanged(sender: UISegmentedControl) {
            switch startStopTracking.selectedSegmentIndex
            {
            case 0:
                manager.startUpdatingLocation()
            case 1:
                manager.stopUpdatingLocation()
            default:
                break;
            }
    }

    @IBAction func clearPath(sender: UIButton) {
        //label.text = String(myLocations.count)
        //stopTracking()
        //theMap.removeOverlay(overlay: MKOverlay) -> Void
    }

    func stopTracking() {
        manager.stopUpdatingLocation()
        myLocations = []
    }

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

        let spanX = 0.007
        let spanY = 0.007
        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.blueColor()
            polylineRenderer.lineWidth = 6
            return polylineRenderer
        }
        return nil
    }
}

最佳答案

试试这个:删除所有

   for poll in mapkit.overlays {

        mapkit.removeOverlay(poll)
    }

关于ios - Xcode Swift 删除 MKPolyline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26857149/

相关文章:

ios - 具有结构的通用函数

ios - 类型 'UITableViewCell' 的值没有成员 'postImageView'

ios - 在带有 Ludei/Cocoon JS 的 IOS 设备上使用 WebGL 不接触 ThreeJS OrbitControls

ios - Android的weightsum在iOS中相当于多少

xcode - 如何在OS X Yosemite中更改WKWebview用户代理?

Xcode 将语义问题显示为错误

iOS 的 UITableView 有问题以编程方式选择多个单元格并显示支票

ios - 将倒数计时器值重置为数组中保存的默认值

ios - Swift - 从另一个类调用 ViewDidAppear

ios - Swift 如何制作像这张图片一样的背景?