我正在尝试为主详细信息应用程序创建一个非常简单的导航视图控制器。我使用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
}
}
我在模拟器中没有得到任何回应,我不确定我哪里出错了。我相信这很快2.任何指导都会受到赞赏。
答案 0 :(得分:0)
当我尝试运行您的代码时,即使位置服务已初始化并(显然)正常工作,我也无法在地图上获得任何内容。
但是,我在控制台中收到以下警告:
此应用尝试访问隐私敏感数据而不使用 用法说明。该应用的Info.plist必须包含一个 NSLocationAlwaysUsageDescription键,其字符串值解释为 用户应用程序如何使用此数据。
因此,将NSLocationAlwaysUsageDescription
添加到plist文件中,您将从位置服务获得更新。