2

Hi so i am having trouble with trying to create this MKPolyline for my ios 8 app. This is suppose to trace the users's location and draw a line where ever it goes. I have been looking at tutorials online and they are using ios 7 or 6. Any help would be great. Thank in advance .

    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)

    }

    }
Icaro
  • 14,585
  • 6
  • 60
  • 75
Brian Chiem
  • 71
  • 1
  • 7

0 Answers0