IOS高德地图开发

来源:互联网 发布:linux重启ftp服务 编辑:程序博客网 时间:2024/04/29 06:21


博客链接:www.goofyy.com/blog

或者百度搜索 goofyy


玩了苹果原生地图,觉得IOS8的原生还是差了那么一点点,对比了一下腾讯的SDK和高德的SDK,还是觉得高德更碉些,第三方地图就先拿高德地图开刀了。

使用高德SDK,首先到高德官网注册一个开发者账号,获取开发者KEY。这些高德LBS开放平台都是有详细教程。小编编就不在这里赘余啦。首先是导入库和开发前简单设置。

高德官网下载高德开发的SDK导入。具体导入的库如下

1.引入地图库&搜索库

左侧目录中选中工程名,在 TARGETS->Build Phases-> Link Binary With Libaries 中点击“+”按钮,在弹出的窗口中点击“Add Other”按钮,选择解压后的 MAMapKit.framework 文件添加到工程中。

搜索库的添加方法同上。

2.引入地图库&搜索库

AMap.bundle资源文件中存储了定位、默认大头针标注视图等图片,可利用这些资源图片进行开发。

左侧目录中选中工程名,在右键菜单中选择Add Files to “工程名”…,从 MAMapKit.framework->Resources 文件夹中选择 AMap.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中。

3.引入系统库

左侧目录中选中工程名,在TARGETS->Build Settings-> Link Binary With Libaries中点击“+”按钮,在弹出的窗口中查找并选择所需的库(见下表),单击“Add”按钮,将库文件添加到工程中。

序号    框架       1.UIKit.framework        2D、3D、Search2.Foundation.framework2D、3D、Search3.CoreGraphics.framework2D、3D、Search4.QuartzCore.framework2D、3D5.OpenGLES.framework3D6.CoreLocation.framework2D、3D7.CoreTelephony.framework2D、3D、Search8.SystemConfiguration.framework2D、3D、Search9.libz.dylib        2D、3D、Search10.libstdc++6.09.dylib2D、3D、Search11.Security.framework2D、3D
说明:

2D表示使用2D栅格地图需要的系统文件,3D表示使用3D矢量地图需要的系统文件、Search表示使用搜索库需要的系统文件。



SystemConfiguration.framework、CoreTelephonySecurity.framework、Security.framework 是为了统计app信息使用。

上面的内容来自高德LBS开发平台,(哈哈,都是重复内容,懒得自己写了,不重复造轮子)

建立桥接文件。创建oc的头文件。随便命名,然后左侧目录中选中工程名,在 TARGETS->Build Phases-> Swift Compiler - Code Generation -> Objective-C Briding Header 中输入桥接文件的路径。Command + b建立连接。 success后我们进入下一步小程序

高德地图显示

在Viewcontroller.swift文件里创建地图显示。


import UIKitlet APIKey = "ebcfb60e7224e45b7058a91cfcac023e"class ViewController: UIViewController ,MAMapViewDelegate {override func viewDidLoad() {        super.viewDidLoad()         MAMapServices.sharedServices().apiKey = APIKey                initMapView()}    func initMapView(){                 mapView = MAMapView(frame: UIScreen.mainScreen().bounds)         mapView!.delegate = self                self.view.addSubview(mapView!)}
这里遵循MAMapViewDelegate的代理协议,配置MAP的APIKEY为我们刚刚在高德官网申请的key,将地图的view加入到当前view,run。success。就可以看到高德地图,高德地图上帮我们自带了比例尺和指南针。是不是好简单。



逆地理编码

所谓逆地理编码就是把经纬度转化为地理位置。

Screen Shot 2015-08-21 at 11.47.10 AM

既然要获取地理的经纬度,那肯定用到定位CLLocation。还记得上一篇IOS原生地图的时候说过,IOS8把地图权限分成了两部分,根据使用情况,在Info.plist里面添加字段。


NSLocationWhenInUseUsageDescription  //用的时候获取位置NSLocationAlwaysUsageDescription    //始终获取用户位置
逆地理编码的基本流程:


(1) 初始化主搜索对象AMapSearchAPI,并继承搜索协议 AMapSearchDelegate 。

(2) 构造 Request 对象,配置搜索参数。

(3) 通过主搜索对象以 Request 对象为参数,发起搜索。

(4) 实现搜索协议中对应的回调函数,通过解析 Response 对象获取搜索结果。
完整代码如下


import UIKit let APIKey = "8a1383b14466a8dbf362f44357c496c0" class ViewController: UIViewController ,MAMapViewDelegate, AMapSearchDelegate{         var mapView:MAMapView?    var search:AMapSearchAPI?    var currentLocation:CLLocation?     override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                 MAMapServices.sharedServices().apiKey = APIKey                 initMapView()                 initSearch()    }         func initMapView(){                 mapView = MAMapView(frame: self.view.bounds)                 mapView!.delegate = self                 self.view.addSubview(mapView!)                 let compassX = mapView?.compassOrigin.x                 let scaleX = mapView?.scaleOrigin.x                 //设置指南针和比例尺的位置        mapView?.compassOrigin = CGPointMake(compassX!, 21)         mapView?.scaleOrigin = CGPointMake(scaleX!, 21)                 // 开启定位        mapView!.showsUserLocation = true                  // 设置跟随定位模式,将定位点设置成地图中心点        mapView!.userTrackingMode = MAUserTrackingModeFollow             }         // 初始化 AMapSearchAPI    func initSearch(){        search = AMapSearchAPI(searchKey: APIKey, delegate: self);     }         // 逆地理编码    func reverseGeocoding(){                 let coordinate = currentLocation?.coordinate         // 构造 AMapReGeocodeSearchRequest 对象,配置查询参数(中心点坐标)        let regeo: AMapReGeocodeSearchRequest = AMapReGeocodeSearchRequest()                 regeo.location = AMapGeoPoint.locationWithLatitude(CGFloat(coordinate!.latitude), longitude: CGFloat(coordinate!.longitude))                 println("regeo :\(regeo)")         // 进行逆地理编码查询        self.search!.AMapReGoecodeSearch(regeo)             }         // 定位回调    func mapView(mapView: MAMapView!, didUpdateUserLocation userLocation: MAUserLocation!, updatingLocation: Bool) {        if updatingLocation {            currentLocation = userLocation.location                 }    }         // 点击Annoation回调    func mapView(mapView: MAMapView!, didSelectAnnotationView view: MAAnnotationView!) {        // 若点击的是定位标注,则执行逆地理编码        if view.annotation.isKindOfClass(MAUserLocation){            reverseGeocoding()        }    }         // 逆地理编码回调    func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {        println("request :\(request)")        println("response :\(response)")                 if (response.regeocode != nil) {                         var title = response.regeocode.addressComponent.city                         var length: Int{                return countElements(title)            }                         if (length == 0){                title = response.regeocode.addressComponent.province            }            //给定位标注的title和subtitle赋值,在气泡中显示定位点的地址信息                  mapView?.userLocation.title = title            mapView?.userLocation.subtitle = response.regeocode.formattedAddress        }     }}

到了这里一个高德SDK简单应用就完成了,下一篇。获取附近的兴趣点。


博客链接:www.goofyy.com/blog

或者百度搜索 goofyy



0 0
原创粉丝点击