定位資訊獲取

1 篇文章 / 0 new
author
定位資訊獲取
1.import CoreLocation;
2.extend CLLocationManagerDelegate
3.宣告變數與初始化
//定位管理器
let locationManager:CLLocationManager = CLLocationManager();
func initLocation() {
    //設置定位服務管理器代理
    locationManager.delegate = self;
    //設置定位進度
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    //更新距離
    //locationManager.distanceFilter = 1;//1 公尺
    //發送授權申請
    locationManager.requestWhenInUseAuthorization();// .requestAlwaysAuthorization()
    if (CLLocationManager.locationServicesEnabled())
    {
        //允許使用定位服務的話,開啟定位服務更新
        locationManager.startUpdatingLocation()
        print("定位開始")
    }
}
4.授權檢查與設定
func checkAuth() {
    if(CLLocationManager.authorizationStatus() == .denied) {
        let aleat = UIAlertController(title: "打開定位開關", message:"定位服務未開啟,請進入系統設置>隱私>定位服務中打開開關,並允許定位服務", preferredStyle: .alert)
        let tempAction = UIAlertAction(title: "取消", style: .cancel) { (action) in
        }
        let callAction = UIAlertAction(title: "立即設置", style: .default) { (action) in
            let url = NSURL.init(string: UIApplicationOpenSettingsURLString)
            if(UIApplication.shared.canOpenURL(url! as URL)) {
                UIApplication.shared.openURL(url! as URL)
            }
        }
        aleat.addAction(tempAction);
        aleat.addAction(callAction);
        self.present(aleat, animated: true, completion: nil)
    }
}
5. 實作 delegate
//定位改變執行,可以得到新位置、舊位置
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //獲取最新的座標
    let currLocation:CLLocation = locations.last!
    let data:String = "經度:\(currLocation.coordinate.longitude)\n緯度:\(currLocation.coordinate.latitude)";
    print(data);
    //獲取海拔:currLocation.altitude
    //獲取水準精度:currLocation.horizontalAccuracy
    //獲取垂直精度:currLocation.verticalAccuracy
    //獲取方向:currLocation.course
    //獲取速度:currLocation.speed
 
    //獲取更多資訊
    CLGeocoder().reverseGeocodeLocation(newLoca, completionHandler: { (pms, err) -> Void in
        if (pms?.last?.location?.coordinate) != nil {
            //取得最後一個地標,地標中存儲了詳細的位址資訊,注意:一個地名可能搜索出多個位址
            let placemark:CLPlacemark = (pms?.last)!
            let location = placemark.location;//位置
            let region = placemark.region;//區域
            let addressDic = placemark.addressDictionary;//詳細位址資訊字典,包含以下部分資訊
            //let name=placemark.name;//地名
            //let thoroughfare=placemark.thoroughfare;//街道
            //let subThoroughfare=placemark.subThoroughfare; //街道相關資訊,例如門牌等
            //let locality=placemark.locality; // 城市
            //let subLocality=placemark.subLocality; // 城市相關資訊,例如標誌性建築
            //let administrativeArea=placemark.administrativeArea; // 州
            //let subAdministrativeArea=placemark.subAdministrativeArea; //其他行政區域資訊
            //let postalCode=placemark.postalCode; //郵編
            //let ISOcountryCode=placemark.ISOcountryCode; //國家編碼
            //let country=placemark.country; //國家
            //let inlandWater=placemark.inlandWater; //水源、湖泊
            //let ocean=placemark.ocean; // 海洋
            //let areasOfInterest=placemark.areasOfInterest; //關聯的或利益相關的地標
            print("\(location),\(region),\(addressDic)");
        }
    })
}
6. 停止定位獲取, 因為GPS功能很耗電可在適當時機加入 locationManager.stopUpdatingLocation();
Free Web Hosting