struct GeoLOCATIOn { var latitude: Double var longitude: Double func distancebetween(other: GeoLOCATIOn) -> Double { let LOCATIOnA = CLLOCATIOn(latitude: self.latitude,longitude: self.longitudE) let LOCATIOnB = CLLOCATIOn(latitude: other.latitude,longitude: other.longitudE) return LOCATIOnA.distanceFromLOCATIOn(LOCATIOnB) } } self.foundLOCATIOns - is an array of these structures
在自定义类中,我接收地图上点的坐标.
var coordinates = self.foundLOCATIOns.map{$0.coordinatE}
然后我在地图上绘制路线
self.polyline = MKPolyline(coordinates: &coordinates,count: coordinates.count) self.mapView.addOverlay(self.polyline,level: MKOverlayLevel.Aboveroads)
要绘制路径,我使用MKMapViewDelegate中的以下方法
func mapView(mapView: MKMapView!,rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { if let polylineOverlay = overlay as? MKPolyline { let render = MKPolylineRenderer(polyline: polylineOverlay) render.strokeColor = UIColor.blueColor() return render } return nil }
首先为源和目标创建相关的MKMapItem,例如:
let geocoder = CLGeocoder() let LOCATIOn = CLLOCATIOn(latitude: sourceLatitude,longitude: sourceLongitudE) geocoder.reverseGeocodeLOCATIOn(LOCATIOn,completionHandler: { (placemarks:[AnyObject]?,error:NSError?) -> Void in if placemarks?.count > 0 { if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark { self.source = MKMapItem(placemark: placemark) } } })
(重复目的地.)
然后@L_634_9@mKRoute,例如:
let request:MKDirectionsrequest = MKDirectionsrequest() // source and desTination are the relevant MKMapItems request.setsource(sourcE) request.setDesTination(desTination) // Specify the transportation type request.transportType = MKDirectionsTransportType.Automobile; // If you're open to getTing more than one route,// requestsAlternateRoutes = true; else requestsAlternateRoutes = false; request.requestsAlternateRoutes = true let directions = MKDirections(request: request) directions.calculateDirectionsWithCompletionHandler ({ (response: MKDirectionsResponse?,error: NSError?) in if error == nil { self.directionsResponse = response // Get whichever currentRoute you'd like,ex. 0 self.route = directionsResponse.routes[currentRoute] as MKRoute } })
然后在检索MKRoute后,您可以将折线添加到地图中,如下所示:
@H_317_8@mapView.addOverlay(route.polyline,level: MKOverlayLevel.Aboveroads)
以上是大佬教程为你收集整理的ios – 在Swift中显示地图上的路线全部内容,希望文章能够帮你解决ios – 在Swift中显示地图上的路线所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。