1. 骑行路径规划调用
let beginLocation = FCMapShareTool.shared._userLocationCoordinate2D
let request = AMapRidingRouteSearchRequest.init()
request.origin = AMapGeoPoint.location(withLatitude: CGFloat(beginLocation?.latitude ?? 0.00 ), longitude: CGFloat(beginLocation?.longitude ?? 0.00))
request.destination = AMapGeoPoint.location(withLatitude: CGFloat(location.latitude ), longitude: CGFloat(location.longitude))
self._searchMap?.aMapRidingRouteSearch(request)
2. 线路绘制到地图详细代码
func onRouteSearchDone(_ request: AMapRouteSearchBaseRequest!, response: AMapRouteSearchResponse!) {
if response.route.paths.count > 0 {
let path = response.route.paths[0]
var corArray = [CLLocationCoordinate2D]()
for (_,step) in path.steps.enumerated() {
let lineArray = step.polyline.split(separator: ";")
for str in lineArray {
let tempCorArray = str.split(separator: ",")
if tempCorArray.count == 2 {
let tempLong = String(tempCorArray[0])
let tempLat = String(tempCorArray[1])
let cor = CLLocationCoordinate2D.init(latitude: CLLocationDegrees(CGFloat(tempLat.decimalStringToDouble() ?? 0.00)), longitude: CLLocationDegrees(CGFloat(tempLong.decimalStringToDouble() ?? 0.00)))
corArray.append(cor)
}
}
}
if var temp = corArray as? [CLLocationCoordinate2D] {
if let tempPoly = _polyLine {
self.mapView.remove(tempPoly)
}
let polyline = MAPolyline.init(coordinates: &temp, count: UInt(temp.count))
_polyLine = polyline
self.mapView.add(polyline)
}
}
}
3. 线路绘制
func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
if let tempOver = overlay as? MAPolyline {
let polygonView = MAPolylineRenderer.init(polyline: tempOver)
polygonView?.lineWidth = 8.0
polygonView?.strokeColor = UIColor.muColor(.blue)
polygonView?.fillColor = UIColor.muColor(.red)
polygonView?.lineJoinType = kMALineJoinRound
polygonView?.lineCapType = kMALineCapRound
return polygonView
}
return nil
}