iOS 8 及以上需要在info.plist文件中添加下面两个属性
  NSLocationWhenInUseUsageDescription 使用应用期间
  NSLocationAlwaysUsageDescription 始终
 
 

  1. import UIKit
  2.  
  3. import CoreLocation //系统定位包
  4.  
  5. class ViewController: UIViewController,CLLocationManagerDelegate {
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9. // Do any additional setup after loading the view, typically from a nib.
  10.  
  11. getCurrentLocation()
  12. }
  13.  
  14. override func didReceiveMemoryWarning() {
  15. super.didReceiveMemoryWarning()
  16. // Dispose of any resources that can be recreated.
  17. }
  18.  
  19. //CodeStart
  20.  
  21. /// 定位对象
  22. let locationManager = CLLocationManager()
  23.  
  24. /// 当前所在城市(默认空)
  25. var currentCity = ""
  26.  
  27. /**
  28. 定位获取所在城市
  29. */
  30. func getCurrentLocation(){
  31.  
  32. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  33.  
  34. locationManager.delegate = self
  35.  
  36. if(UIDevice.currentDevice().systemVersion>="8.0"){
  37.  
  38. //locationManager.requestAlwaysAuthorization()
  39.  
  40. locationManager.requestWhenInUseAuthorization()
  41.  
  42. }
  43.  
  44. locationManager.startUpdatingLocation()
  45. }
  46.  
  47. /**
  48. 定位失败
  49. */
  50. func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
  51. println(error.code)
  52. }
  53.  
  54. /**
  55. 定位完成调用
  56. */
  57. func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
  58.  
  59. let cloc = locations.last as! CLLocation
  60.  
  61. let geoCoder = CLGeocoder()
  62.  
  63. var error = NSError()
  64.  
  65. geoCoder.reverseGeocodeLocation(cloc, completionHandler: { (placeMarks:[AnyObject]!, error:NSError!) -> Void in
  66. if error == nil {
  67.  
  68. var placeMark: AnyObject = placeMarks[0]
  69.  
  70. var locationInfo:Dictionary = placeMark.addressDictionary
  71.  
  72. var locationCity = locationInfo["State"] as! String
  73.  
  74. if self.currentCity != locationCity{
  75.  
  76. self.currentCity = locationCity
  77.  
  78. println("当前定位城市:\(self.currentCity)")
  79.  
  80. }
  81.  
  82. }
  83.  
  84. })
  85.  
  86. manager.stopUpdatingLocation()
  87.  
  88. }
  89.  
  90. }

swift 定位的更多相关文章

  1. swift 定位 根据定位到的经纬度转换城市名

    好久没写随笔了   最近这段时间项目有点紧  天天在加班  国庆 一天假都没放  我滴娃娃   好啦  牢骚就不发了  毕竟没有什么毛用    待我那天闲了专门写一篇吐槽的随笔  

  2. 使用CoreLocation进行定位(Swift版)

    在应用开发中,很多情况需要我们获取到当前的位置和高度信息,方便搜索周边,查看周边相同应用等,一切与定位有关的都得使用CoreLocation库,而且,系统是不允许第三发定位的,当然可以使用第三方对其封 ...

  3. Swift基础--定位

    // // ViewController.swift // JieCoreLocation // // Created by jiezhang on 14-10-4. // Copyright (c) ...

  4. Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)

    CoreLocation是iOS中一个提供设备定位的框架.通过这个框架可以实现定位处理,从而获取位置数据,比如经度.纬度.海拔信息等.   1,定位精度的设置 定位服务管理类CLLocationMan ...

  5. Swift - 给表格UITableView添加索引功能(快速定位)

    像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组.   要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理 ...

  6. 基于swift MKMapkit 开发的地图定位导航

    //  DTOneViewController.swift //  Mapper-JSON // //  Created by kcl on 16/8/8. //  Copyright © 2016年 ...

  7. swift 相机、相册、定位的权限判断

    //是否开启相机权限 func IsOpenCamera() -> Bool{ let authStatus = AVCaptureDevice.authorizationStatus(for: ...

  8. 定位框一闪而过 iOS Swift

    需求:获取经纬度. 方案:我自定义了一个类模块CLLocationModule.swift 备注以下代码里 let IS_IOS8 = (UIDevice.currentDevice().system ...

  9. 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码

    在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...

随机推荐

  1. javascript中利用柯里化函数实现bind方法

    柯理化函数思想:一个js预先处理的思想:利用函数执行可以形成一个不销毁的作用域的原理,把需要预先处理的内容都储存在这个不销毁的作用域中,并且返回一个小函数,以后我们执行的都是小函数,在小函数中把之前预 ...

  2. Nodejs报错集

    1.ReferenceError: userModule is not defined A:1>检查app.js文件中是否调用userModule所在的文件(const userModule=r ...

  3. WinForm中MouseEnter和MouseLeave混乱的问题

    MouseEnter+MouseLeave不行,我用了MouseMove+MouseLeave,效果一样 最近做个聊天的系统,仿照qq的界面设计,像qq聊天界面中字体.表情.截图等图片,鼠标放上去显示 ...

  4. PHP简单文件上传

    一个简单的PHP上传文件的例子: upload.html <html> <body> <form action="upload.php" method ...

  5. 【图像】Matlab图像标定工具箱

    参考教程: Matlab工具箱教程  http://www.vision.caltech.edu/bouguetj/calib_doc/ 摄像机模型  http://oliver.zheng.blog ...

  6. 【A Global Line Matching Algorithm for 2D Laser Scan Matching in Regular Environment】

    只看了前面的部分,灭有看实验,觉得整体风格比较傻白甜,与我的想法不谋而合.简单明了,用起来应该比较方便. 初步探测:如果有直线,就给线性插值一下. 分级聚类:利用简单的阈值给聚类了一下,分成了段段. ...

  7. C++输入cout与输出cin

    输入和输出并不是C++语言中的正式组成成分.C和C++本身都没有为输入和输出提供专门的语句结构.输入输出不是由C++本身定义的,而是在编译系统提供的I/O库中定义的.C++的输出和输入是用" ...

  8. Oracle 表连接

    Oracle 表之间的连接分为三种: 1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制)      (2)右外连接(右边的表不加限制)      (3)全外连接(左右两表都不 ...

  9. css划隔横线的两种方法

    css划隔横线的两种方法  方法一:用DIV,代码如下:(推荐此方法)    <div style="width:800px;height:1px;margin:0px auto;pa ...

  10. 开源代码Window下搭建rtmp流媒体服务器

    合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综合:有这样需求,将摄像头 ...