本文展示的是在IOS開(kāi)發(fā)中調(diào)用高德地圖進(jìn)行駕車(chē)路徑繪制,開(kāi)發(fā)語(yǔ)言是Swift。
IOS高德地圖集成請(qǐng)看:IOS集成高德地圖Api
使用路徑規(guī)劃功能需要集成高德地圖的搜索功能。
pod 'AMapSearch'
定義AMapSearchAPI
定義主搜索對(duì)象 AMapSearchAPI ,并繼承搜索協(xié)議。
import AMapSearchKit
var searchApi:AMapSearchAPI!
構(gòu)造 AMapSearchAPI
searchApi=AMapSearchAPI()
searchApi.delegate=self
//起點(diǎn)終點(diǎn)
startCoordinate = CLLocationCoordinate2DMake(39.910267, 116.370888)
destinationCoordinate = CLLocationCoordinate2DMake(39.989872, 116.481956)
設(shè)置駕車(chē)線路規(guī)劃參數(shù)
//請(qǐng)求參數(shù)類(lèi)
let request=AMapDrivingCalRouteSearchRequest()
//設(shè)置起點(diǎn)
request.origin = AMapGeoPoint.location(withLatitude: CGFloat(startCoordinate.latitude), longitude: CGFloat(startCoordinate.longitude))
//設(shè)置終點(diǎn)
request.destination = AMapGeoPoint.location(withLatitude: CGFloat(destinationCoordinate.latitude), longitude: CGFloat(destinationCoordinate.longitude))
//顯示字段類(lèi)型
request.showFieldType = AMapDrivingRouteShowFieldType.init(rawValue: AMapDrivingRouteShowFieldType.cost.rawValue|AMapDrivingRouteShowFieldType.tmcs.rawValue|AMapDrivingRouteShowFieldType.navi.rawValue|AMapDrivingRouteShowFieldType.cities.rawValue|AMapDrivingRouteShowFieldType.polyline.rawValue)!
發(fā)起駕車(chē)路線規(guī)劃
//發(fā)起駕車(chē)路線規(guī)劃
searchApi.aMapDrivingV2RouteSearch(request)
在回調(diào)中處理數(shù)據(jù)
實(shí)現(xiàn)代理方法onRouteSearchDone
//路徑搜索結(jié)果
func onRouteSearchDone(_ request: AMapRouteSearchBaseRequest!, response: AMapRouteSearchResponse!) {
// 取出第一種路線方案
let stringWithOptional = response.route.paths.first?.polyline!
let distance=response.route.paths.first?.distance
let time=response.route.paths.first?.duration
print("距離:\(distance!)米,預(yù)計(jì)耗時(shí):\(time!)秒")
let result = convertToArray(stringWithOptional)
if var temp = result {
let polyline = MAPolyline.init(coordinates: &temp, count: UInt(temp.count))
mapView.add(polyline)
}
}
//轉(zhuǎn)數(shù)組
func convertToArray(_ coordinatesString: String!) -> [CLLocationCoordinate2D]? {
// 去掉 "Optional(" 和 ")" 前綴和后綴
let cleanedString = coordinatesString.replacingOccurrences(of: "Optional(\"", with: "").replacingOccurrences(of: "\")", with: "")
var corArray = [CLLocationCoordinate2D]()
let coordinatesArray = cleanedString.components(separatedBy: ";")
for coordinate in coordinatesArray {
let components = coordinate.components(separatedBy: ",")
if components.count == 2, let longitude = Double(components[0]), let latitude = Double(components[1]) {
let cor = CLLocationCoordinate2D.init(latitude: CLLocationDegrees(CGFloat(latitude)), longitude: CLLocationDegrees(CGFloat(longitude)))
corArray.append(cor)
} else {
return nil
}
}
return corArray
}
路線繪制
arrowTexture是圖片資源文件,按照官方文檔的說(shuō)法:紋理圖片須是正方形,寬高是2的整數(shù)冪,如64*64,否則無(wú)效;若設(shè)置了紋理圖片,設(shè)置線顏色、連接類(lèi)型和端點(diǎn)類(lèi)型將無(wú)效。
//路徑繪制代理
func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
if let tempOver = overlay as? MAPolyline {
let polygonView = MAPolylineRenderer.init(polyline: (overlay as! MAPolyline))
// 參數(shù)設(shè)置
polygonView?.lineWidth = 10.0
polygonView?.strokeImage=UIImage.init(resource: ImageResource.arrowTexture)
return polygonView
}
return nil
}
另外,要是有箭頭的話,記得要是箭頭向下的,向上的話實(shí)際顯示箭頭會(huì)反過(guò)來(lái),奇奇怪怪的
起點(diǎn)終點(diǎn)圖標(biāo)設(shè)置(可跳過(guò))
需要實(shí)現(xiàn)這個(gè)代理,不設(shè)置也會(huì)有默認(rèn)的大頭針圖標(biāo)
default_common_route_startpoint_normal、default_common_route_endpoint_normal是圖標(biāo)資源文件
//圖標(biāo)設(shè)置代理
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
let pointReuseIndetifier = "pointReuseIndetifier"
var annotationView: MAAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier)
if annotationView == nil {
annotationView = MAAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
annotationView!.canShowCallout = true
annotationView!.isDraggable = false
}
annotationView!.image = nil
if annotation.title == "起點(diǎn)" {
annotationView!.image = UIImage(named: "default_common_route_startpoint_normal")
}
else if annotation.title == "終點(diǎn)" {
annotationView!.image = UIImage(named: "default_common_route_endpoint_normal")
}
return annotationView
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-799777.html
結(jié)果
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-799777.html
到了這里,關(guān)于IOS-高德地圖路徑繪制-Swift的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!