CoreLocation简单应用

1、获取locationManager

let locationManager: CLLocationManager = CLLocationManager()

2、设置locationManager的精度

locationManager.desiredAccuracy = kCLLocationAccuracyBest

3、如果是ios8需要

locationManager.requestAlwaysAuthorization()

4、开始监听数据的更新

locationManager.startUpdatingLocation()

5、添加locationManagerDelegate的回调函数,并将经纬度打出

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){

        var location:CLLocation = locations[locations.count - 1] as CLLocation

        if(location.horizontalAccuracy > 0){

            println("\(location.coordinate.latitude)")

            println("\(location.coordinate.longitude)")

                       

        }

6、设置locationManager的代理

locationManager.delegate = self

7、locationManager错误处理

func locationManager(manager: CLLocationManager!, rangingBeaconsDidFailForRegion region: CLBeaconRegion!, withError error: NSError!){

            println(error)

    }

8、关闭数据更新

locationManager.stopUpdatingLocation()

        

    }

posted on 2014-09-30 20:37  SCaptain  阅读(235)  评论(0编辑  收藏  举报

导航