Swift Ios 8 MKPolylineRenderer追踪位置

时间:2015-06-08 04:14:24

标签: xcode swift mapkit trace polyline

嗨,所以我在尝试为我的ios 8应用程序创建这个MKPolyline时遇到了麻烦。这是为了跟踪用户的位置并在任何地方绘制一条线。我一直在线查看教程,他们正在使用ios 7或6.任何帮助都会很棒。预先感谢 。

    import UIKit
    import MapKit
    import CoreLocation
    class SecondViewController: UIViewController , CLLocationManagerDelegate , MKMapViewDelegate{
    @IBOutlet weak var map: MKMapView!
    let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
            var currentLocation  = CLLocation()
            self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.requestWhenInUseAuthorization()
            self.locationManager.startUpdatingLocation()
        var pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude , locationManager.location.coordinate.longitude)
        var objectAnnotation = MKPointAnnotation()
        objectAnnotation.coordinate = pinLocation
        objectAnnotation.title = "Current Location"
        self.map.addAnnotation(objectAnnotation)
}
    func addPolyLineToMap(locations: [CLLocation!])
    {
        var coordinates = locations.map({ (location: CLLocation!) -> CLLocationCoordinate2D in
            return location.coordinate
        })
        var geodesic = MKGeodesicPolyline(coordinates: &coordinates[0], count: 2)
        map.addOverlay(geodesic , level: MKOverlayLevel.AboveRoads )
    }
    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolyline {
            var polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = UIColor.blueColor()
            polylineRenderer.lineWidth = 4
            return polylineRenderer
        }
        return nil
    }
        func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
        {
            println("Error: " + error.localizedDescription)
        }

   func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        //centering location 

        let location = locations.last as CLLocation

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    var latDelta:CLLocationDegrees = 0.01

        var longDelta:CLLocationDegrees = 0.01

        var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    `var pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude , location.coordinate.longitude)
       map.setRegion(region, animated: true)

    addPolyLineToMap(location)

    }

    }

0 个答案:

没有答案