在两个纬度和经度之间绘制折线
Posted
技术标签:
【中文标题】在两个纬度和经度之间绘制折线【英文标题】:draw polyline between two latitude and longitude 【发布时间】:2018-06-26 10:49:54 【问题描述】:我为源和目的地传递了两个 lat 和 long,但没有在两点之间绘制折线。总是出错。下面的代码我在源坐标中传递我的当前位置,在 destCoordinates 中传递旅行位置。我想在 sourceCoordinates 到 destCoordinates 之间绘制折线..
if(CLLocationManager.locationServicesEnabled())
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
//let sourceCoordinates = locationManager.location?.coordinate
let sourceCoordinates = CLLocationCoordinate2DMake(19.115950,72.856448)
let destCoordinates = CLLocationCoordinate2DMake(19.119670,72.853616)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source=sourceItem
directionRequest.destination=destItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler:
response,error in
guard let response = response else
if let error = error
print("something wrong")
return
let route = response.routes[0]
self.upcoming_info_map.add(route.polyline,level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.upcoming_info_map.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
)
绘制折线我使用这个函数
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
let renderer = MKPolygonRenderer(overlay:overlay)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 5.0
return renderer
【问题讨论】:
你设置代理了吗 如果您打印出错误,它会清楚地显示“步行路线不可用”,因此请更改方向类型并检查 对于苹果地图中没有的印度方向,请尝试其他纬度和经度 【参考方案1】:使用此方法获取位置列表并绘制折线
func addPolyLineToMap(googlemaplist: [CLLocation?])
var coordinates = googlemaplist.map( (location: CLLocation!) -> CLLocationCoordinate2D in
return location.coordinate
)
print("locatios count")
print(googlemaplist.count)
var polyline = MKPolyline(coordinates: &coordinates, count: googlemaplist.count)
DispatchQueue.main.async
self.MapKit.add(polyline)
【讨论】:
以上是关于在两个纬度和经度之间绘制折线的主要内容,如果未能解决你的问题,请参考以下文章