Swift 使用CoreLocation获取定位与位置信息

news/2024/7/1 3:08:45
大多数情况下APP会在开启应用的时候获取当前的位置,所以我写在APPDelegate里

第一步

import CoreLocationvar locationManager = CLLocationManager()
第二步

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {//开启定位loadLocation()  return true
}
第三步 实现代理方法

extension AppDelegate: CLLocationManagerDelegate
{//打开定位
func loadLocation()
{locationManager.delegate = self//定位方式locationManager.desiredAccuracy = kCLLocationAccuracyBest//iOS8.0以上才可以使用if(UIDevice.currentDevice().systemVersion >= "8.0"){//始终允许访问位置信息locationManager.requestAlwaysAuthorization()//使用应用程序期间允许访问位置数据locationManager.requestWhenInUseAuthorization()}//开启定位locationManager.startUpdatingLocation()
}//获取定位信息
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {//取得locations数组的最后一个let location:CLLocation = locations[locations.count-1]currLocation = locations.last!//判断是否为空if(location.horizontalAccuracy > 0){lat = Double(String(format: "%.1f", location.coordinate.latitude))long = Double(String(format: "%.1f", location.coordinate.longitude))print("纬度:\(long!)")print("经度:\(lat!)")LonLatToCity()//停止定位locationManager.stopUpdatingLocation()}}//出现错误
func locationManager(manager: CLLocationManager, didFinishDeferredUpdatesWithError error: NSError?) {print(error)
}///将经纬度转换为城市名
func LonLatToCity() {let geocoder: CLGeocoder = CLGeocoder()geocoder.reverseGeocodeLocation(currLocation) { (placemark, error) -> Void inif(error == nil){let array = placemark! as NSArraylet mark = array.firstObject as! CLPlacemark//城市var city: String = (mark.addressDictionary! as NSDictionary).valueForKey("City") as! String//国家let country: NSString = (mark.addressDictionary! as NSDictionary).valueForKey("Country") as! NSString//国家编码let CountryCode: NSString = (mark.addressDictionary! as NSDictionary).valueForKey("CountryCode") as! NSString//街道位置let FormattedAddressLines: NSString = (mark.addressDictionary! as NSDictionary).valueForKey("FormattedAddressLines")?.firstObject as! NSString//具体位置let Name: NSString = (mark.addressDictionary! as NSDictionary).valueForKey("Name") as! NSString//省var State: String = (mark.addressDictionary! as NSDictionary).valueForKey("State") as! String//区let SubLocality: NSString = (mark.addressDictionary! as NSDictionary).valueForKey("SubLocality") as! NSString//如果需要去掉“市”和“省”字眼State = State.stringByReplacingOccurrencesOfString("省", withString: "")let citynameStr = city.stringByReplacingOccurrencesOfString("市", withString: "")}else{print(error)}}}}


http://lihuaxi.xjx100.cn/news/235269.html

相关文章

源码解析之–YYAsyncLayer异步绘制

前言 YYAsyncLayer是异步绘制与显示的工具。最初是从YYKitDemo中接触到这个工具,为了保证列表滚动流畅,将视图绘制、以及图片解码等任务放到后台线程,在YYAsyncLayer之前还是想从YYKitDemo中性能优化说起,虽然些跑题了… YYKitDem…

排序算法 - 堆排序

堆排序是指利用堆这种数据结构所设计的一种排序算法。 类型:选择排序时间复杂度(最坏):O(nlogn)时间复杂度(最好):O(nlogn)时间复杂度(平均):O(nlogn)空间复杂…

swift3.0阿里百川反馈

闲言少叙 直接上不熟 1.导入自己工程阿里百川demo中的Util文件,并引用其中的头文件 2.剩余就是swift3.0代码.在自己需要的地方书写 (前提是你已经申请了APPKey) 3.代码 //调用意见反馈 func actionOpenFeedback(){ //key self.appKey "此处填写自己申请的key" s…

Notification与多线程

前几天与同事讨论到Notification在多线程下的转发问题,所以就此整理一下。 先来看看官方的文档,是这样写的: In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, whi…

activemq 消息阻塞优化和消息确认机制优化

一、消息阻塞优化 1.activemq消费者在从待消费队列中获取消息是会先进行预读取,默认是1000条(prefetch1000)。这样很容易造成消息积压。 2.可以通过设置prefetch的默认值来调整预读取条数,java代码如下 //设置预读取为1ActiveMQPr…

自己常用网站

在线批量生产iOS图标 http://www.atool.org/ios_logo.ph iOS MVVM RAC从框架到实战 http://www.cnblogs.com/leixu/articles/5344335.html cocoapods安装流程及使用 http://blog.csdn.net/p_igmihu/article/details/52858375 Swift - 使用Alamofire通过HTTPS进行网络请求…

数据库索引-基本知识

为什么80%的码农都做不了架构师?>>> 数据库索引--基本知识 有许多因素会影响数据库性能。最明显的是数据量:您拥有的数据越多,数据库的速度就越慢。虽然有很多方法可以解决性能问题,但主要的解决方案是正确索引数据库…

RxSwift Runtime分析(利用OC消息转发实现IOS消息拦截)原理同ReactiveCocoa

简要介绍:这是一篇介绍IOS消息拦截的文章,来源于对RxSwift源码的分析,其原理是利用Object-c的消息转发(forwardInvocation:)来实现(ReactiveCocoa中也是这个原理,而且是RXSwift借鉴的RAC和MAZeroingWeakRef),阅读本文章…