在IOS和OS X設(shè)備上都可以使用定位服務(wù),它們共享同一套框架,即Core Location。使用這個(gè)框架來(lái)獲得設(shè)備的位置是非常方便的,你不需要關(guān)心設(shè)備使用什么硬件方式去獲取位置,蜂窩基站,Wi-Fi,GPS。框架會(huì)根據(jù)你設(shè)備的硬件情況和你對(duì)定位精度的設(shè)置來(lái)自動(dòng)選擇定位方式。
獲取權(quán)限
位置是用戶的隱私,所以對(duì)設(shè)備位置的請(qǐng)求必須獲得用戶的許可。在OS X上,你不需要額外的配置,只要系統(tǒng)發(fā)現(xiàn)你在獲取設(shè)備的位置,就回詢問(wèn)用戶是否同意。在IOS上,這個(gè)過(guò)程會(huì)稍微復(fù)雜一些,有兩種權(quán)限,一種是允許應(yīng)用在前臺(tái)活動(dòng)時(shí)獲取位置信息,一種是在后臺(tái)也可以獲取位置信息。這兩種都需要在info.plist中配置,對(duì)應(yīng)的鍵分別是:NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription它們的值可以隨便填,不過(guò)這個(gè)值會(huì)在彈出的詢問(wèn)用戶的彈窗里顯示。
配置CLLocationManager對(duì)象
//對(duì)應(yīng)兩種獲取的權(quán)限:只在前臺(tái)獲取位置信息requestWhenInUseAuthorization
//在后臺(tái)也獲取requestAlwaysAuthorization
//這個(gè)if判斷是為了兼容IOS8以下,沒(méi)有這個(gè)方法
if locationManager.respondsToSelector("requestWhenInUseAuthorization") {
locationManager.requestWhenInUseAuthorization()
}
//設(shè)置定位精度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//設(shè)置設(shè)備移動(dòng)多長(zhǎng)的距離才調(diào)用位置更新方法
locationManager.distanceFilter = 1000
locationManager.startUpdatingLocation()
locationManager.delegate = self
實(shí)現(xiàn)代理方法
//獲取用戶位置成功
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let newLocation = locations.last{
myGPSs.append(myGPS(coorX: newLocation.coordinate.latitude, coorY: newLocation.coordinate.longitude, initTitle: "你的位置", initSubtitle: "你在這里",url: NSURL(string: "http://img3.douban.com/view/photo/photo/public/p1864484723.jpg"),initName: "Fixed"))
//如果獲得到的精度大于0則停止獲取位置信息,否則很費(fèi)電,這個(gè)值為-1則定位不可信
if newLocation.horizontalAccuracy>0 {
locationManager.stopUpdatingLocation()
}
//在獲得的位置周圍生成一個(gè)以精度為半徑的覆蓋物,這個(gè)必須實(shí)現(xiàn)一個(gè)mapView的代理方法才會(huì)生效,這個(gè)代理方法決定了如何渲染這個(gè)覆蓋物
let overlay = MKCircle(centerCoordinate: newLocation.coordinate, radius: newLocation.horizontalAccuracy)
mapView.addOverlay(overlay)
mapView.addAnnotations(myGPSs)
mapView.showAnnotations(myGPSs, animated: true)
} else {
print("No location found")
}
}
//獲得用戶位置失敗
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
}
地理編碼
Core Location提供了將地址轉(zhuǎn)換為經(jīng)緯度和經(jīng)緯度轉(zhuǎn)換為地址的方法,當(dāng)讓這個(gè)功能需要訪問(wèn)蘋果的服務(wù)器。使用CLGeocoder類
geocoder.reverseGeocodeLocation(newLocation)
{ (placemarks:[CLPlacemark]?, error:NSError?) -> Void in
if error == nil {
//對(duì)于一個(gè)坐標(biāo),可能會(huì)返回多個(gè)地址
let placemark = placemarks![0] as CLPlacemark
let address = NSString(format: "%@ %@, %@, %@ %@",
placemark.country!,
placemark.administrativeArea!,
placemark.locality!,
placemark.thoroughfare!,
placemark.subThoroughfare!)
self.addressLabel.stringValue = address as String
} else {
self.addressLabel.stringValue = "Fail to Find an Address"
}
}
監(jiān)測(cè)用戶出入?yún)^(qū)域
首先注冊(cè)一個(gè)區(qū)域
//監(jiān)測(cè)用戶出入該區(qū)域
let location = CLLocationCoordinate2DMake(39.96, 116.35)
let region = CLCircularRegion(center: location, radius: 1000, identifier: "BUPT")
locationManager.startMonitoringForRegion(region)
實(shí)現(xiàn)代理方法
//用戶進(jìn)入監(jiān)測(cè)區(qū)域
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("enter \(region.identifier)")
}
//用戶離開監(jiān)測(cè)區(qū)域
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
print("leave \(region.identifier)")
}