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

本文详细介绍了iOS开发的基础知识、工具使用、实践经验,涵盖了从Objective-C到Swift的编程语言转换,以及Xcode的高效应用。通过实例解析,帮助开发者快速上手iOS应用开发,提升开发效率。


代码如下:


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
这样就可以编写定位代码并定位了,这样会有是否允许定位的提示



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值