关于这个问题的SO的所有答案都是使用Objective-c,或者使用Swift 2.我有两个点,第一个坐标是(lat,long),第二个坐标是坐标。纬度,coordinates.longitude)。这是我迄今为止所能得到的,但我仍有两个问题:
Value of type 'MKDirectionsRequest' has no member 'setSource'
Value of type 'MKDirectionsRequest' has no member 'setDestination'
我从这个页面获得此代码的任何方式:http://studyswift.blogspot.com/2014/10/mkdirections-draw-route-from-location.html
到目前为止,这是我的代码:
@IBOutlet weak var myMap: MKMapView!
var myRoute : MKRoute?
在viewDidLoad
内我有以下内容:
let directionsRequest = MKDirectionsRequest()
let markFirstPoint = MKPlacemark(coordinate: CLLocationCoordinate2DMake(lat, long), addressDictionary: nil)
let markSecondPoint = MKPlacemark(coordinate: CLLocationCoordinate2DMake(coordinates.latitude, coordinates.longitude), addressDictionary: nil)
directionsRequest.setSource(MKMapItem(placemark: markSecondPoint))
directionsRequest.setDestination(MKMapItem(placemark: markFirstPoint))
directionsRequest.transportType = MKDirectionsTransportType.automobile
let directions = MKDirections(request: directionsRequest)
directions.calculate { (response:MKDirectionsResponse!, error: Error!) -> Void in
if error == nil {
self.myRoute = response.routes[0] as? MKRoute
self.myMap.add((self.myRoute?.polyline)!)
}
}
在viewDidLoad
我之后:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
var myLineRenderer = MKPolylineRenderer(polyline: (myRoute?.polyline)!)
myLineRenderer.strokeColor = UIColor.red
myLineRenderer.lineWidth = 3
return myLineRenderer
}
有什么方法可以让这段代码起作用吗?
答案 0 :(得分:3)
只需查看MKDirectionsRequest
的文档即可。有source
属性和destination
属性。
行:
directionsRequest.setSource(MKMapItem(placemark: markSecondPoint))
directionsRequest.setDestination(MKMapItem(placemark: markFirstPoint))
成为:
directionsRequest.source = MKMapItem(placemark: markSecondPoint)
directionsRequest.destination = MKMapItem(placemark: markFirstPoint)