第三天:Swift利用CoreLocation获取当前地址

参考链接:https://www.jianshu.com/p/ade69f95bffc

              

 1 import UIKit
 2 import CoreLocation
 3 
 4 class ViewController: UIViewController, CLLocationManagerDelegate {
 5     
 6     @IBOutlet weak var showLocationBtn: UIButton!
 7     @IBOutlet weak var locationLabel: UILabel!
 8     
 9     var locationManager: CLLocationManager!
10     
11     override func viewDidLoad() {
12         super.viewDidLoad()
13         // Do any additional setup after loading the view, typically from a nib.
14         
15         UIApplication.shared.statusBarStyle = .lightContent
16         
17     }
18 
19     @IBAction func showLocationAction(_ sender: UIButton) {
20         
21         locationManager = CLLocationManager()
22         locationManager.delegate = self
23         
24         locationManager.desiredAccuracy = kCLLocationAccuracyBest
25         locationManager.requestAlwaysAuthorization()
26         locationManager.startUpdatingLocation()
27     }
28     
29     override func didReceiveMemoryWarning() {
30         super.didReceiveMemoryWarning()
31         // Dispose of any resources that can be recreated.
32     }
33 
34 }
35 
36 extension ViewController {
37     func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
38         self.locationLabel.text = "Error while updating location: " + error.localizedDescription
39     }
40     
41     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
42         CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {
43             (placemarks, error) -> Void in
44             
45                 if error != nil {
46                 self.locationLabel.text = "Reverse geocoder failed with error:" + error!.localizedDescription
47                 return
48             }
49             
50             if placemarks!.count > 0 {
51                 let pm = placemarks![0]
52                 self.displayLocationInfo(pm)
53             } else {
54                 self.locationLabel.text = "Error existed in the data received from geocoder"
55             }
56         })
57     }
58     
59     func displayLocationInfo(_ placemark: CLPlacemark?) {
60         guard let containsPlacemark = placemark else {return}
61         
62         locationManager.stopUpdatingLocation()
63         
64         let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
65         let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
66         let adminstrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
67         let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
68         
69         self.locationLabel.text = postalCode! + " " + locality!
70         self.locationLabel.text?.append("\n")
71         self.locationLabel.text?.append(adminstrativeArea! + ", " + country!)
72     }
73 }

 

转载于:https://www.cnblogs.com/chmhml/p/8300925.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值