绘制折线 MapBox,iOS
Draw polyline MapBox, iOS
我正在使用 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)
}
})
}
我正在使用 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)
}
})
}