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. Pdf 字段加粗相关资料

    http://blog.csdn.net/lx_lhy/article/details/5603073 http://www.codeweblog.com/stag/setfieldproperty- ...

  2. ruby 字符串学习笔记3

    ascii转字符或者字符串转ascii "a".ord # => 97 "!".ord # => 33 "\n".ord # = ...

  3. wall 和panel有啥区别

    Panel指的是一个面板,用它来对一些控件进行分组,就像组合框控件,即Visual Studio里面用的Group Box Control:而在一些软件界面里面也可以表现为工具条,比如编辑工具条.文件 ...

  4. java异常分类(运行时异常,可检查异常)

    NullPointerException:是运行时异常(RuntimeException),也叫非检查异常 所以我们抛出该类异常实例时,方法声明处无需添加throws来列举该类异常的抛出,编译器在编译 ...

  5. luvit 初尝鲜

    官网:http://luvit.io/ Luvit is an attempt to do something crazy by taking node.js' awesome architectur ...

  6. using inno setup uninstall default icon

    If you set SetupIconFile then the Uninstall Exe File (e.g. unins000.exe) will have exactly same icon ...

  7. (medium)LeetCode 207.Course Schedule

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Spark是一种分布式的计算方案

    Spark的安装基于HDFS,所以我们要设置hadoop的配置文件,所以spark的存储不是其主要的功能点,而spark作为分布式生态中的角色是一种计算模式(其他 的计算 模式,比如MR,Storm, ...

  9. c/c++笔记

    string 若要根据字典序比较string类型的大小,只需要用><=就可以啦 例如: string s1="abcz"; string s2="abcd&q ...

  10. (Loadrunner)Abnormal termination, caused by mdrv process termination.(转)

    Load generator跑了太多用户导致CPU和内存爆满,进程无法处理请求 确认自定义的代码是否释放内存 合理调整或增加思考时间 关闭extended log 尽量避免使用Load generat ...