首页 > 解决方案 > 根据以前的位置绘制折线,以跟踪用户路径

问题描述

我在 Xcode 中使用了以下代码来显示我过去位置的折线。但它似乎没有出现。任何帮助将不胜感激。

我不确定我将位置存储在数组中的方法是否错误,或者我显示折线的代码是否错误。

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


    let location = locations[0]

    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    mapKitView.setRegion(region, animated:true)


    print (location.speed)
    print (location.altitude)
    print (location.coordinate)
    print (location.course) //Direction you are heading in NSEW


    altitude.text = String(location.altitude)
    mgrs.text = String(location.coordinate.latitude)

    if (storedLocations.count > 1)
    {
        let sourceIndex = storedLocations.count - 1
        let destinationIndex = storedLocations.count - 2

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

        mapKitView.add(polyline)

        print("running")
    }


    self.mapKitView.showsUserLocation = true



}



func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.blue
    renderer.lineWidth = 5.0

    return renderer
}


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

标签: iosswiftmapkitpolyline

解决方案


推荐阅读