iOS获取当前城市
1.倒入头文件
#import <CoreLocation/CoreLocation.h>
2.实现定位协议CLLocationManagerDelegate
@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 >
)
{
CLPlacemark *placemark = [array
objectAtIndex:];
//将获得的全部信息显示到label上
NSLog(@"%@",placemark.name);
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,仅仅能通过获取省份的方法来获得(假设city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
self.cityName = city;
}
else if (error ==
nil && [array count] ==
)
{
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获取当前城市的更多相关文章
- iOS 获取当前城市
1.倒入头文件 #import <CoreLocation/CoreLocation.h> 2.实现定位协议CLLocationManagerDelegate 3.定义定位属性 @pro ...
- ios 获取通讯录的所有信息
iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBoo ...
- iOS获取设备唯一标识的8种方法
8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...
- iOS 获取文件的目录路径的几种方法 [转]
iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...
- iOS获取设备型号、装置类型等信息
iOS获取设备型号.设备类型等信息 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然而他们都陆陆续续倒在了苹果的门下.苹果目前提供了2个方法供App获取设 ...
- C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- IOS 获取最新设备型号方法
1.IOS 获取最新设备型号方法列表最新对照表:http://theiphonewiki.com/wiki/Models方法: #import "sys/utsname.h” struct ...
- iOS获取汉字的拼音
在iOS开发中经常涉及到汉字的排序,最常见的就是需要根据首字母的字符顺序排列,比如常见的通讯录等.总结出来,大致可以分为两种方法,其中参考文献[1]中提供的方法十分复杂,利用查表的方法是先,并且代码量 ...
随机推荐
- Mysql乐观锁与悲观锁
乐观锁和悲观锁是两种常见的资源并发锁设计思路,也是并发编程中一个非常重要的基础理念. Mysql的悲观锁 什么是悲观锁(Pessimistic Lock): 悲观锁的特点是先获取锁,再进行业务操作,即 ...
- Redis的工作流程
从图上可以看出,当一个客户端访问服务器的时候,客户端请求会先到达Nginx,由Nginx负责对数据进行分发,上传到多个服务器,当用户访问到tomcat1的时候,会进行登陆验证并将session放入se ...
- spring security学习
https://www.cnblogs.com/leihenqianshang/articles/5313159.html
- pyqt pyside QPushButton 图标(icon)大小自适应设置
pyqt pyside QPushButton 图标(icon)大小自适应设置 img = QtGui.QImage(r'd:/test.png') pixmap = QtGui.QPixmap(im ...
- Maya cmds pymel 获取安装选择顺序选择的物体
Maya cmds pymel 获取安装选择顺序选择的物体 import maya.cmds as cmds 先设置选择顺序 cmds.selectPref(trackSelectionOrder = ...
- Java版统计文件中的每个单词出现次数
正则表达式之Pattern和Matcher,请参见转载博客 http://www.cnblogs.com/haodawang/p/5967219.html 代码实现: import java.i ...
- Pedestrian Attributes Recognition Paper List
Pedestrian Attributes Recognition Paper List 2018-12-22 22:08:55 [Note] you may also check the upda ...
- C#Workbooks 对象的 Open 方法参数说明
打开一个工作簿. excelApp.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword,IgnoreRea ...
- 4535 ACM 礼尚往来 数学排列组合
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4535 题意:每个礼物都不相同的组合个数 数学规律: 将每个女友排序为1···n,对应的女友送男友的礼物排序 ...
- 超详细Gitlab Runner环境配置中文教程
配置GitlabRunner环境 GitLab Runner 是一个开源项目, 它用来运行你定制的任务(jobs)并把结果返回给 GitLab. GitLab Runner 配合GitLab CI(G ...