首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MapView中绘制用户路由

如何在MapView中绘制用户路由
EN

Stack Overflow用户
提问于 2018-06-28 10:42:30
回答 1查看 545关注 0票数 0

如何在“`mapView”中绘制用户的路由?我的地图是工作,但路线不是绘制。没有错误。例如:用户路线

我的代码:

代码语言:javascript
复制
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
    }


}

但不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-29 05:01:17

我想你是用下面的代表得到最后一个职位的?

代码语言:javascript
复制
func locationManager(manager: CLLocationManager!,
                     didUpdateToLocation newlocation: CLLocation!,
                     fromLocation oldLocation: CLLocation!) {

在iOS 6中,不推荐使用上面的委托。现在应该使用以下方法:

代码语言:javascript
复制
func locationManager(_ manager: CLLocationManager,
                     didUpdateLocations locations: [CLLocation]) {

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

代码语言:javascript
复制
locations.lastObject

尝试使用以下代码绘制用户的路由:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51080854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档