2022-03-31
1、先初始化 AMapSearchAPI ,继承 AMapSearchDelegate 这个代理
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var segment: UISegmentedControl!
var _startPoi = BMKPoiInfo()
var _endPoi = BMKPoiInfo()
///起点坐标
var route:AMapRoute?
var type:TransferType?
//高德地图
var searchGD : AMapSearchAPI!
var list = [AMapTransit]()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "查询结果"
// Do any additional setup after loading the view.
tableView.register(UINib(nibName: "TransferListTableViewCell", bundle: nil), forCellReuseIdentifier: "TransferListCell")
segment.addTarget(self, action: #selector(segmentedControlChange(_ :)), for: .valueChanged)
searchGD = AMapSearchAPI.init()
searchGD.delegate = self
reloadData()
}
@objc func segmentedControlChange(_ segmented: UISegmentedControl) {
if segmented.selectedSegmentIndex == 0 {
print("第0个啊哈哈")
searchRight(0)
}
else if segmented.selectedSegmentIndex == 1 {
print("第1个啊哈哈")
searchRight(2)
}
else {
print("其他啊哈哈")
searchRight(3)
}
}
func reloadData() {
list.removeAll()
list.append(contentsOf: route?.transits ?? [])
self.tableView.reloadData()
}
2、 发起搜索 代理是路线查询的 数据
extension TransferListViewController:AMapSearchDelegate{
//MARK: - 高德地图 搜索线路规划
func searchRight(_ tag:NSInteger) {
// 驾车导航策略,默认策略为0。
// ///公交换乘策略([default = 0])\n 0-最快捷模式;\n 1-最经济模式;\n 2-最少换乘模式;\n 3-最少步行模式;\n 4-最舒适模式;\n 5-不乘地铁模式
let request = AMapTransitRouteSearchRequest()
request.origin = AMapGeoPoint.location(withLatitude: CGFloat(_startPoi.pt.latitude), longitude: CGFloat(_startPoi.pt.longitude))
request.destination = AMapGeoPoint.location(withLatitude: CGFloat(_endPoi.pt.latitude), longitude: CGFloat(_endPoi.pt.longitude))
request.strategy = tag
request.requireExtension = true
request.city = "龙口市"
searchGD.aMapTransitRouteSearch(request)
}
func onRouteSearchDone(_ request: AMapRouteSearchBaseRequest!, response: AMapRouteSearchResponse!) {
if response.count > 0 {
//解析response获取路径信息
self.route = response.route
self.reloadData()
}else{
showToast("抱歉,未找到结果")
}
}
}
extension TransferListViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tran = list[indexPath.row]
let time = Date.timeStampToString(timeStamp: tran.duration).replacingOccurrences(of: ":", with: "小时")
let cell = tableView.dequeueReusableCell(withIdentifier: "TransferListCell") as! TransferListTableViewCell
cell.titleLabel.text = tran.segments.first?.buslines.first!.name
//tran.distance/1000
cell.subLabel.text = "\(time)分钟 | \(String(format: "%.1f", CGFloat(tran.distance)/CGFloat(1000)) )公里 | \(String(format: "%.1f", CGFloat(tran.walkingDistance)/CGFloat(1000)))公里"
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tran = list[indexPath.row]
let channel = TransferStepDetailsViewController(nibName: "TransferStepDetailsViewController", bundle: nil)
channel.tran = tran
channel._startPoi = _startPoi
channel._endPoi = _endPoi
self.navigationController?.pushViewController(channel, animated: true)
}
}
网友评论