首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

IOS 定位CoreLocation

import CoreLocation 2 class ViewController:UIViewController,CLLocationManagerDelegate 3 var locationManager:CLLocationManager! 4 var label:UILabel! 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 9 locationManager = CLLocationManager() 10 locationManager.delegate = self 11 locationManager.desiredAccuracy = kCLLocationAccuracyBest 12 locationManager.distanceFilter = 1000.0 13 14 label = UILabel(frame:CGRect(x:20, y:80, width: 280, height:100)) 15 label.numberOfLines = 2 16 label.backgroundColor = UIColor.brown 17 self.view.addSubview(label) 18 19 if CLLocationManager.authorizationStatus() == .notDetermined { 20 locationManager.requestAlwaysAuthorization() 21 } 22 } 23 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 24 switch status { 25 case .denied: 26 print(“用户拒绝您对地理设备使用的请求。”) 27 break; 28 default: 29 manager.startUpdatingLocation() 30 break; 31 } 32 } 33 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 34 locationManager.stopUpdatingLocation() 35 36 let location:CLLocation = locations[0] 37 let latitude = location.coordinate.latitude 38 let longitude = location.coordinate.longitude 39 40 label.text = “经度:(longitude)\n 纬度:(latitude)” 41 }

02
领券