Xcode Swift Ios 8 MKPolylineRenderer至跟踪位置
您好,我在尝试为ios 8应用程序创建此多段线时遇到问题。假设这是为了追踪用户的位置,并在用户所处的位置画一条线。我一直在网上看教程,他们正在使用ios 7或6。任何帮助都会很好。先谢谢你Xcode Swift Ios 8 MKPolylineRenderer至跟踪位置,xcode,swift,mapkit,trace,polyline,Xcode,Swift,Mapkit,Trace,Polyline,您好,我在尝试为ios 8应用程序创建此多段线时遇到问题。假设这是为了追踪用户的位置,并在用户所处的位置画一条线。我一直在网上看教程,他们正在使用ios 7或6。任何帮助都会很好。先谢谢你 import UIKit import MapKit import CoreLocation class SecondViewController: UIViewController , CLLocationManagerDelegate , MKMapViewDelegate
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)
}
}
在哪里调用函数addPolyLineToMap?你从哪里得到用户位置?我猜我忘了添加更新位置功能。我现在才加的