Podfile文件
platform :ios, '8.0'
target 'yourapp' do
pod 'AMapLocation'
end
然后,pod install
Info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>家教100 请求后台定位权限</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
说明:第一项是请求后台定位权限的描述,第二项是在ios9中必须要使用的
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
忘了高德的接口是http的了,也不知道ios10要怎么适配,知道的人告诉我一下
AMapDemoSwift-Bridging-Header.h 文件
#ifndef AMapDemoSwift_Bridging_Header_h
#define AMapDemoSwift_Bridging_Header_h
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#endif /* AMapDemoSwift_Bridging_Header_h */
注意做好桥接
AppDelegate.swift文件中
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
AMapServices.sharedServices().apiKey = "yourappkey"
return true
}
在实现文件中需要实现AMapLocationManagerDelegate协议,主要的方法是
var locationManager:AMapLocationManager!
// 配置,并非delegate中的方法
private func configLocationManager() {
self.locationManager = AMapLocationManager()
self.locationManager.delegate = self
self.locationManager.pausesLocationUpdatesAutomatically = false
self.locationManager.allowsBackgroundLocationUpdates = false
}
// 发生了错误
func amapLocationManager(manager:AMapLocationManager, didFailWithError error:NSError) {
print("error:\(error)")
}
// 更新定位
func amapLocationManager(manager:AMapLocationManager, didUpdateLocation location:CLLocation) {
print("lat:\(location.coordinate.latitude),lng:\(location.coordinate.longitude)")
self.locationManager.stopUpdatingLocation()
}
在启动的时候调用
self.configLocationManager()
self.locationManager.startUpdatingLocation()
暂时就是这样吧