以下结构用于存储坐标
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{self.polyline = MKpolyline(coordinates: &coordinates,count: coordinates.count) self.mapVIEw.addOverlay(self.polyline,level: MKOverlayLevel.AboveRoads).coordinate}
然后我在地图上绘制路线
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}
要绘制路径,我使用MKMapVIEwDelegate中的以下方法
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) } } })
而不是铺设在道路上的实际路线,我只得到两点之间的直线.
如何显示实际路线?
首先为源和目标创建相关的MKMAPItem,例如:
let request:MKDirectionsRequest = MKDirectionsRequest()// source and destination are the relevant MKMAPItemsrequest.setSource(source)request.setDestination(destination)// Specify the transportation typerequest.transportType = MKDirectionsTransportType.automobile;// If you're open to getting more than one route,// requestsAlternateRoutes = true; else requestsAlternateRoutes = false;request.requestsAlternateRoutes = truelet 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,例如:
mapVIEw.addOverlay(route.polyline,level: MKOverlayLevel.AboveRoads)
然后在检索MKRoute后,您可以将折线添加到地图中,如下所示:
总结以上是内存溢出为你收集整理的ios – 在Swift中显示地图上的路线全部内容,希望文章能够帮你解决ios – 在Swift中显示地图上的路线所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)