根据以前的位置绘制一条折线,以跟踪用户路径

时间:2018-07-30 11:26:56

标签: ios swift mapkit polyline

我在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.
}

0 个答案:

没有答案