我正在尝试为我的主从应用程序创建一个非常简单的导航视图控制器。我正在使用 swift (2) 并具有以下代码:
import MapKit
import UIKit
import CoreData
import CoreLocation
class DirectionsViewController: UIViewController, CLLocationManagerDelegate,MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var point1 = MKPointAnnotation()
var point2 = MKPointAnnotation()
var myRoute : MKRoute?
override func viewDidLoad() {
super.viewDidLoad()
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
point1.coordinate = CLLocationCoordinate2DMake(locValue.latitude, locValue.longitude) //code to sync in current location
point1.title = "You are here"
point1.subtitle = "Start"
mapView.addAnnotation(point1)
// to test location - print("locations = \(locValue.latitude) \(locValue.longitude)")
point2.coordinate = CLLocationCoordinate2DMake(40.22396739999999, -74.0303199)
point2.title = "First Pentecostal Church"
point2.subtitle = "Neptune, NJ"
mapView.addAnnotation(point2)
mapView.centerCoordinate = point2.coordinate
mapView.delegate = self
//Span of the map
mapView.setRegion(MKCoordinateRegionMake(point2.coordinate, MKCoordinateSpanMake(0.7,0.7)), animated: true)
var directionsRequest = MKDirectionsRequest()
let markUser = MKPlacemark(coordinate: CLLocationCoordinate2DMake(point1.coordinate.latitude, point1.coordinate.longitude), addressDictionary: nil)
let markFPC = MKPlacemark(coordinate: CLLocationCoordinate2DMake(point2.coordinate.latitude, point2.coordinate.longitude), addressDictionary: nil)
directionsRequest.setSource(MKMapItem(placemark: markUser))
directionsRequest.setDestination(MKMapItem(placemark: markFPC))
directionsRequest.transportType = MKDirectionsTransportType.Automobile
var directions = MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler { (response:MKDirectionsResponse!, error: NSError!) -> Void in
if error == nil {
self.myRoute = response.routes[0] as? MKRoute
self.mapView.addOverlay(self.myRoute?.polyline)
}
}
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
var myLineRenderer = MKPolylineRenderer(polyline: myRoute?.polyline!)
myLineRenderer.strokeColor = UIColor.redColor()
myLineRenderer.lineWidth = 3
return myLineRenderer
}
}
我在模拟器中没有得到任何响应,我不确定我哪里出错了。我相信这是 swift 2. 任何指导将不胜感激。
当我尝试运行您的代码时,我也没有在地图上看到任何内容,即使位置服务已初始化并且(显然)正在工作。
但是,我确实在控制台中收到以下警告:
此应用程序试图在没有使用说明的情况下访问隐私敏感数据。应用程序的 Info.plist 必须包含一个 NSLocationAlwaysUsageDescription 键和一个字符串值,向用户解释应用程序如何使用这些数据。
因此,将 加入NSLocationAlwaysUsageDescription
到您的 plist 文件中,您将从位置服务中获得更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句