1.倒入头文件

#import <CoreLocation/CoreLocation.h>

2.实现定位协议CLLocationManagerDelegate

3.定义定位属性 

@property(nonatomic,retain)CLLocationManager *locationManager;

4.开始定位

- (void)locate

{

// 判断定位操作是否被允许

if([CLLocationManager locationServicesEnabled]) {

self.locationManager = [[CLLocationManager alloc] init] ;

self.locationManager.delegate = self;

}else {

//提示用户无法进行定位操作

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:

@"提示" message:@"定位不成功 ,请确认开启定位" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alertView show];

}

// 开始定位

[self.locationManager startUpdatingLocation];

}

 

5.实现定位协议回调方法

#pragma mark - CoreLocation Delegate

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation

CLLocation *currentLocation = [locations lastObject];

// 获取当前所在的城市名

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

//根据经纬度反向地理编译出地址信息

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)

{

if (array.count > 0)

{

CLPlacemark *placemark = [array objectAtIndex:0];

//将获得的所有信息显示到label上

NSLog(@"%@",placemark.name);

//获取城市

NSString *city = placemark.locality;

if (!city) {

//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

city = placemark.administrativeArea;

}

self.cityName = city;

}

else if (error == nil && [array count] == 0)

{

NSLog(@"No results were returned.");

}

else if (error != nil)

{

NSLog(@"An error occurred = %@", error);

}

}];

//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新

[manager stopUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager

didFailWithError:(NSError *)error {

if (error.code == kCLErrorDenied) {

// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在

}

}

iOS 获取当前城市的更多相关文章

  1. iOS获取当前城市

    1.倒入头文件 #import <CoreLocation/CoreLocation.h> 2.实现定位协议CLLocationManagerDelegate 3.定义定位属性 @prop ...

  2. ios 获取通讯录的所有信息

    iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBoo ...

  3. iOS获取设备唯一标识的8种方法

    8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...

  4. iOS 获取文件的目录路径的几种方法 [转]

    iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...

  5. iOS获取设备型号、装置类型等信息

    iOS获取设备型号.设备类型等信息 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然而他们都陆陆续续倒在了苹果的门下.苹果目前提供了2个方法供App获取设 ...

  6. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...

  7. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  8. IOS 获取最新设备型号方法

    1.IOS 获取最新设备型号方法列表最新对照表:http://theiphonewiki.com/wiki/Models方法: #import "sys/utsname.h” struct ...

  9. iOS获取汉字的拼音

    在iOS开发中经常涉及到汉字的排序,最常见的就是需要根据首字母的字符顺序排列,比如常见的通讯录等.总结出来,大致可以分为两种方法,其中参考文献[1]中提供的方法十分复杂,利用查表的方法是先,并且代码量 ...

随机推荐

  1. 在Linux环境下安装和配置phpmyadmin

    phpmyadmin是一种MySQL的图形化管理工具,该工具允许你在web界面上管理你的mysql数据库,不可谓不方便快捷. 此次安装与配置是在centos 6.4系统下,该系统已部署lnmp环境.关 ...

  2. IceGrid负载均衡部署 z

    [IceGrid负载均衡部署步骤]1.环境主机1:IP=192.168.0.239,上面部署注册表服务器registry和节点node1,registry和node1运行在同一进程中:主机2:IP=1 ...

  3. chkconfig : No such file or directory

    sys_version:12.04LTS For example: #chkconfig --level mysql on /sbin/insserv:No such file or director ...

  4. 高级Java开发工程师

  5. Java基础-布局

  6. jenkins参数化构建过程

    http://www.cnblogs.com/meitian/p/5492457.html 1,首先增加Jenkisn插件 https://wiki.jenkins-ci.org/display/JE ...

  7. SQL锁表解决并发性

    在数据库开发过程中,不得不考虑并发性的问题,因为很有可能当别人正在更新表中记录时,你又从该表中读数据,那你读出来的数据有可能就不是你希望得到的数据.可以说有些数据同时只能有一个事物去更新,否则最终显示 ...

  8. [ActionScript 3.0] AS3中Loader无法彻底卸载

    我测试发现,实例化的Loader无法彻底卸载,同行有没有办法,求赐教! import flash.display.Loader; import flash.net.URLRequest; import ...

  9. java 错误之:Exception in thread "main" java.lang.NoClassDefFoundError

    Exception in thread "main" java.lang.NoClassDefFoundError: PointTest 环境变量的问题,把环境变量设置好了就可以了 ...

  10. Redis多机功能介绍

    Redis多机功能目的:以单台Redis服务器过渡到多台Redis服务器 Redis单机在生产环境中存在的问题 1.内存容量不足 Redis使用内存来存书数据库中的数据,但是对于一台机器来说,硬件的内 ...