swift - 如何在MapView中绘制用户的路线

标签 swift mapkit

如何在`mapView中绘制用户的路线? 我的 map 可以工作,但路线没有绘制。 没有错误。 例如: User's route

我的代码:

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!


    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self



            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()

        locationManager.startUpdatingLocation()
        locationManager.startUpdatingHeading()

        mapView.delegate = self
        mapView.showsUserLocation = true
        mapView.mapType = MKMapType(rawValue: 0)!
        mapView.userTrackingMode = MKUserTrackingMode(rawValue: 2)!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager!, didUpdateToLocation newlocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
        print("present location : \(newlocation.coordinate.latitude), \(newlocation.coordinate.longitude)")
        if let oldLocationNew = oldLocation as CLLocation?{
            let oldCoordinates = oldLocationNew.coordinate
            let newCoordinates = newlocation.coordinate
            var area = [oldCoordinates, newCoordinates]
            var polyline = MKPolyline(coordinates: &area, count: area.count)
            mapView.add(polyline)
        }
    }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            let pr = MKPolylineRenderer(overlay: overlay)
            pr.strokeColor = UIColor.red
            pr.lineWidth = 5
        return pr
    }


}

但是这不起作用。

最佳答案

我假设您使用以下委托(delegate)来获得最后一个位置?

func locationManager(manager: CLLocationManager!,
                     didUpdateToLocation newlocation: CLLocation!,
                     fromLocation oldLocation: CLLocation!) {

上面的委托(delegate)在 iOS 6 中已被弃用。现在应该使用以下内容:

func locationManager(_ manager: CLLocationManager,
                     didUpdateLocations locations: [CLLocation]) {

为了获取最后一个位置,只需获取数组的最后一个对象:

locations.lastObject

尝试以下代码来绘制用户的路线:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let newLocation = locations.last else {
        return
    }

    guard let oldLocation = oldLocation else {
        // Save old location
        self.oldLocation = newLocation
        return
    }

    let oldCoordinates = oldLocation.coordinate
    let newCoordinates = newLocation.coordinate
    var area = [oldCoordinates, newCoordinates]
    let polyline = MKPolyline(coordinates: &area, count: area.count)
    mapView.add(polyline)

    // Save old location
    self.oldLocation = newLocation
}

关于swift - 如何在MapView中绘制用户的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51080854/

相关文章:

ios - 无法在 Swift 中的 TableView 的另一个 View Controller 中显示值

ios - 仅在 Swift 4.2 中上传包含多部分表单数据的图像

ios - 使用 Firebase 检索多查询

iOS MKMapView 不在模拟器中显示 map

ios - viewForAnnotation 详细披露更改当前位置以及引脚

ios - 如何再次调用权限表 CoreMotion

uitableview - 隐藏在大灰色框后面的 TableView

ios - 如何使用 Swift 访问 Firebase 数据库中的一组键值?

ios - mapkit xcode 8 中的 onclick 事件弹出窗口

ios - 向 MKUserTrackingBarButtonItem 添加操作