swift学习笔记(七)(定位)

7 篇文章 0 订阅


代码如下:


import UIKit

import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var locationLabel: UILabel!  // sb里的定位显示label

    

    var locationManager: CLLocationManager!

    

    override func viewDidLoad() {

        super.viewDidLoad()

    }

    

    override func preferredStatusBarStyle() -> UIStatusBarStyle {

        return UIStatusBarStyle.LightContent

    }

    

    @IBAction func myLocationButtonDidTouch(sender: AnyObject) { // sb里的定位触发按钮

        

        locationManager = CLLocationManager()

        locationManager.delegate = self

        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        locationManager.requestAlwaysAuthorization()

        locationManager.startUpdatingLocation()

        

    }


    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {

        

      self.locationLabel.text = "Error while updating location " + error.localizedDescription

        

    }

    

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

        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in

            

            if (error != nil) {

               self.locationLabel.text = "Reverse geocoder failed with error" + error!.localizedDescription

                return

            }

            

            if placemarks!.count > 0 {

                let pm = placemarks![0

                self.displayLocationInfo(pm)

            } else {

               self.locationLabel.text = "Problem with the data received from geocoder"

            }

        })

    }

    

    func displayLocationInfo(placemark: CLPlacemark?) {

        if let containsPlacemark = placemark {

            //stop updating location to save battery life

            locationManager.stopUpdatingLocation()

            

            let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""

            let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""

            let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""

            let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""

            

            self.locationLabel.text = locality! +  postalCode! +  administrativeArea! +  country!

        }

        

    }


}


1.定位要用到coreLocation.framework, 记得在info.plist中添加键值如下:
NSLocationAlwaysUsageDescription  String    where are you
NSLocationWhenInUseUsageDescription  String   Allow to use location and can send it to me
这样就可以编写定位代码并定位了,这样会有是否允许定位的提示



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值