gpt4 book ai didi

ios - 获取多个坐标之间的方向

转载 作者:可可西里 更新时间:2023-11-01 02:01:26 24 4
gpt4 key购买 nike

我想在许多坐标(lat,long)之间获得一个独特的方向。例如,我有一个 API 获取请求返回 200 坐标,我必须在 map 中的这 200 个点之间创建一个唯一的方向。我该怎么做(200 点的数组按数学距离排序)。示例:

[9.6247279999999993, 46.170966100000001]
[9.6244014, 46.171050399999999]
[9.6240834999999993, 46.171273900000003]
[9.6240570000000005, 46.171284999999997]

最佳答案

属性:-

var mapView: MKMapView!
var polyLine: MKPolyline!
var lineView: MKPolylineView!

从纬度和经度创建坐标数组:

  var coordinateArray = [CLLocationCoordinate2D]()
for obj in arrayOfLatlong {
let coordinate = CLLocationCoordinate2DMake(lat1, lon1);
coordinateArray.append(coordinate)
}

//添加折线:-

 polyLine = MKPolyline(coordinates: coordinateArray, count: coordinateArray.count)
mapView?.visibleMapRect = polyLine.boundingMapRect
//If you want the route to be visible
mapView?.add(polyLine)

在委托(delegate)方法中:-

 func mapView(_ mapView: MKMapView, viewFor overlay: MKOverlay) -> 
MKOverlayView {
if overlay == polyLine {
if lineView == nil {
lineView = MKPolylineView(polyline: routeLine)
lineView.fillColor = UIColor.red
lineView.strokeColor = UIColor.red
lineView.lineWidth = 5
}
return lineView
}
return nil

创建可步行路径:-

   // Source
let coordinate = CLLocationCoordinate2D(latitude: lat, longitude:
long)
let placemark: MKPlacemark = MKPlacemark(coordinate: coordinate)
let source = MKMapItem(placemark: placemark)

//Destination
let destination = MKMapItem(placemark: placemark)

let request:MKDirectionsRequest = MKDirectionsRequest()
request.source = source
request.destination = destination
// Specify the transportation type
request.transportType = MKDirectionsTransportType.walking;

request.requestsAlternateRoutes = true

let directions = MKDirections(request: request)

directions.calculate (completionHandler: {
(response: MKDirectionsResponse?, error: NSError?) in
if error == nil {
let directionsResponse = response
// Get whichever currentRoute you'd like, ex. 0
self.arrayOfRoutes = directionsResponse?.routes
}
} as! MKDirectionsHandler)

从 arrayOfRoutes 获取路线并显示在 mapview 上。

mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)

关于ios - 获取多个坐标之间的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46036746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com