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

import UIKit

import CoreLocation //系统定位包

class ViewController: UIViewController,CLLocationManagerDelegate {

    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. getCurrentLocation()
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} //CodeStart /// 定位对象
let locationManager = CLLocationManager() /// 当前所在城市(默认空)
var currentCity = "" /**
定位获取所在城市
*/
func getCurrentLocation(){ locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.delegate = self if(UIDevice.currentDevice().systemVersion>="8.0"){ //locationManager.requestAlwaysAuthorization() locationManager.requestWhenInUseAuthorization() } locationManager.startUpdatingLocation()
} /**
定位失败
*/
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error.code)
} /**
定位完成调用
*/
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { let cloc = locations.last as! CLLocation let geoCoder = CLGeocoder() var error = NSError() geoCoder.reverseGeocodeLocation(cloc, completionHandler: { (placeMarks:[AnyObject]!, error:NSError!) -> Void in
if error == nil { var placeMark: AnyObject = placeMarks[0] var locationInfo:Dictionary = placeMark.addressDictionary var locationCity = locationInfo["State"] as! String if self.currentCity != locationCity{ self.currentCity = locationCity println("当前定位城市:\(self.currentCity)") } } }) manager.stopUpdatingLocation() } }

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. js实现对移动设备的检测

    <script type="text/javascript"> if (browserRedirect()) { location.href = 'http:/phon ...

  2. 锁相关知识 & mutex怎么实现的 & spinlock怎么用的 & 怎样避免死锁 & 内核同步机制 & 读写锁

    spinlock在上一篇文章有提到:http://www.cnblogs.com/charlesblc/p/6254437.html  通过锁数据总线来实现. 而看了这篇文章说明:mutex内部也用到 ...

  3. ubuntu16.04 + ubuntu + apache2 配置apache解析php

    给apache安装php扩展:  sudo apt-get install libapache2-mod-php 注:这是apache解析php文件的关键,光修改配置文件不安装扩展是不起作用的. 目录 ...

  4. redhat samba匿名登录服务器搭建

    smb服务器只需要yum install samba 安装起,并修改配置文件就可以匿名使用了.   smb配置文件,这里允许匿名登录.红色部分代表我共享出来文件夹参数code [root@localh ...

  5. JQuery_表单选择器

    表单作为HTML 中一种特殊的元素,操作方法较为多样性和特殊性 开发者不但可以使用之前的常规选择器或过滤器,也可以使用jQuery 为表单专门提供的选择器和过滤器来准确的定位表单元素. 一.常规选择器 ...

  6. guardian keytab生成不了

    vim /var/kerberos/krb5kdc/kadm5.acl 将*e改成* /etc/init.d/kadmin restart 重启kadmin

  7. Hadoop基础学习框架

    我们主要使用Hadoop的2个部分:分布式文件存储系统(HDFS)和MapReduce计算模型. 关于这2个部分,可以参考一下Google的论文:The Google File System 和 Ma ...

  8. 根据不同的ie的版本号,制定不同的方法

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. How threads differ from processes

    How threads differ from processes Threads differ from traditional multitasking operating system proc ...

  10. js判断是否是微信浏览器

    <script type="text/javascript"> window.onload = function(){ if(isWeiXin()){ var p = ...