gpt4 book ai didi

ios - 绘制折线 MapBox, iOS

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

我正在使用 Mapbox iOS SDK 创建一个带有路线的简单应用程序,并试图找出如何在没有 geojson 的情况下绘制多段线。首先,我尝试使用此方法获取路线:

 func getRoute(directionsRequest: MBDirectionsRequest){

let directionsRequest = MBDirectionsRequest(sourceCoordinate: pointOne.coordinate, destinationCoordinate: pointTwo.coordinate)
directionsRequest.transportType = .Automobile

let directions = MBDirections(request: directionsRequest, accessToken: "pk.eyJ1IjoidXJiaWNhIiwiYSI6ImNpb2xkNndvMjAwMW13cW1ibmY4Z2t3NHcifQ.3wadKQBcytWcJVY1eUSVWQ")


directions.calculateDirectionsWithCompletionHandler({ (response: MBDirectionsResponse?, error: NSError?) -> Void in

if error != nil {
print(error)
} else {
self.myRoute = response?.routes.last
print(self.myRoute?.destination.coordinate)
self.drawRoute(self.myRoute!)
}

})
}

然后尝试绘制路线,但没有成功。

func drawRoute(myRoute: MBRoute){
let waypoints = myRoute.waypoints

var coordinates: [CLLocationCoordinate2D] = []

for point in waypoints {

let coordinate = CLLocationCoordinate2DMake(point.coordinate.latitude, point.coordinate.longitude)
coordinates.append(coordinate)
}

let line = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))

dispatch_async(dispatch_get_main_queue(), {
[unowned self] in
self.mapView.addAnnotation(line)
print(line)
})
}

最佳答案

在这种情况下,您不应该将代码分成两个方法,结果应该如下所示

 directions.calculateDirectionsWithCompletionHandler({

(response, error) in

if let routeOne = response?.routes.first {

let steps = routeOne.legs.first!.steps

for step in steps {
self.myTourArray.append(step)
self.myTourArrayPoints.append(step.maneuverLocation)
}

self.myTourline = MGLPolyline(coordinates: &self.myTourArrayPoints, count: UInt(self.myTourArray.count))
self.mapView.addAnnotation(self.myTourline)
}
})
}

关于ios - 绘制折线 MapBox, iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435087/

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