高德地图poi检索的封装。并实现一个输入地址关键字,边检索的功能,地址模糊输入功能。

我们经常会用到poi检索输入地址。在我以前的博客做了百度地图工具的封装。这次项目,用到高德地图。这次我做了一个poi检索的简单的封装。

import UIKit


class POISearchManager: NSObject{

    static let sharedInstance = POISearchManager()

    

    var ID:Int?

    var _CallBack:((AMapPOISearchResponse)->Void)?

    var DataArray:[AMapPOI]! = [AMapPOI]()

    var search:AMapSearchAPI={

        let search = AMapSearchAPI()

        

        return search!

        

        

    }()

    var request:AMapPOIKeywordsSearchRequest = AMapPOIKeywordsSearchRequest()

    private override init() {

    

    }

    // 私有化init方法

    func getPoiSearch(city:NSString,key:String,page:NSInteger,callBack:@escaping ((AMapPOISearchResponse)->Void)){

        _CallBack = callBack

        self.search.delegate = self

        request.city = city as String?

       request.keywords = key

       request.page = page

       search.aMapPOIKeywordsSearch(request)

    }

    

}

extension POISearchManager:AMapSearchDelegate{

    

    func onPOISearchDone(_ request: AMapPOISearchBaseRequest!, response: AMapPOISearchResponse!) {

        _CallBack!(response)

    

    }

}

高德地图的关键字的poi检索就封装了。这么几十行。

下面我开始使用这个工具。做poi检索了。

import UIKit

let addressCell = "addressCell"

class AddressViewController: UIViewController {

    

    

     var DataArray:[AMapPOI]! = [AMapPOI]()

    

    var callBack:((AMapPOI)->())?


    var naviView:AddressNaviView={

        let view = AddressNaviView.LoadFromNib()

        

        return view

    }()

    var tableview:UITableView = {

      let tableView = UITableView(frame: CGRect.init(x: 0, y: 64+IPXstatusHeight, width: KScreenW, height: KScreenH-64-IPXstatusHeight))

      tableView.tableFooterView = UIView()

       //tableView.backgroundColor = UIColor.red

      return tableView

    }()

    override func viewDidLoad() {

        super.viewDidLoad()

        setUpUI()

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    

    }

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(true)

        self.navigationController?.navigationBar.isHidden = true

    }

    override func viewWillDisappear(_ animated: Bool) {

        super.viewWillDisappear(true)

        self.navigationController?.navigationBar.isHidden = false

    }

}

extension AddressViewController{

    func setUpUI(){

        view.backgroundColor = UIColor.white

        view.addSubview(naviView)

        naviView.callBack = {(text) in

            //调用poi检索工具类实现模糊输入。

            POISearchManager.sharedInstance.getPoiSearch(city: "深圳市", key: text, page: 1) { (response) in

                self.DataArray = response.pois

                self.tableview.reloadData()

                    }

        }

        

        naviView.operation = {(status) in

            if status {

                

            }else{

               self.navigationController?.popViewController(animated: true)

            }

        }

        view.addSubview(tableview)

        tableview.delegate = self

        tableview.dataSource = self

        tableview.register(UITableViewCell.self, forCellReuseIdentifier: addressCell)

    }

}

extension AddressViewController:UITableViewDataSource,UITableViewDelegate{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        

        return self.DataArray.count

        

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: addressCell, for: indexPath)

        if cell == nil  {

             cell = UITableViewCell.init(style: .default, reuseIdentifier: addressCell)

        }

        

        cell.imageView?.image = UIImage.init(named: "location")

        cell.textLabel?.text = self.DataArray[indexPath.row].name

        cell.textLabel?.textColor = UIColor.gray

        

        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let poi = self.DataArray[indexPath.row]

        if self.callBack != nil  {

            callBack!(poi)

            self.dismiss(animated: true, completion: nil)

        }

    }

}



上面的代码运行效果如图所示。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值