iOS 获得当前经纬度和城市
1.引入CoreLocation.framework,#import <CoreLocation/CoreLocation.h>,添加委托CLLocationManagerDelegate
2.
{
float Currentlat,Currentlon;
CLLocationManager *locationManager;
UILabel *cityLabel;
}
3.viewDidLoad里添加
if ([CLLocationManager locationServicesEnabled] == NO) {
NSLog(@"没有GPS服务");
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有GPS服务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alertView show];
}else{
CLLocationManager *locmanager = [[CLLocationManager alloc]init];//先定义一个cllocationmanager的实例
locationManager=locmanager;
[locationManager requestAlwaysAuthorization];//相对应info.plist中的NSLocationAlwaysUsageDescription键
[locmanager setDelegate:self]; //设置代理为本身
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];//设置精确度为最准确
locmanager.distanceFilter = 1000.0f;
[locationManager startUpdatingLocation];
}
4.
//ios6.0以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
CLLocationCoordinate2D coor = newLocation.coordinate;
Currentlat = coor.latitude;
Currentlon = coor.longitude;
NSLog(@"当前维度ios7 %f 当前经度 %f",Currentlat,Currentlon);
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSDictionary *test = [placemark addressDictionary];
// Country(国家) State(城市) SubLocality(区)
cityLabel.text=[test objectForKey:@"State"];
}
}];
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"定位失败");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"定位失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
}
5.
ios8必须注意:
iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息
- (void)requestAlwaysAuthorization;
(2)使用应用程序期间允许访问位置数据
- (void)requestWhenInUseAuthorization;
在运行开始更新前加下面这句
[locationManager requestAlwaysAuthorization];//相对应info.plist中的NSLocationAlwaysUsageDescription键
2、在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
这两个键的值就是授权alert的描述,示例配置如下[勾选Show Raw Keys/Values后进行添加]:

额外附赠:
五、根据两点坐标计算两点之间的距离,此方法为苹果自带方法,亲测速度比高德API速度快很多,但是数据与高德API得到的不一样,准确度本人未能证实
//第一个坐标
CLLocation *current=[[CLLocation alloc] initWithLatitude:32.178722 longitude:119.508619];
//第二个坐标
CLLocation *before=[[CLLocation alloc] initWithLatitude:32.206340 longitude:119.425600];
// 计算距离
CLLocationDistance meters=[current distanceFromLocation:before];
iOS 获得当前经纬度和城市的更多相关文章
- php根据经纬度获取城市名
/*php根据经纬度获取城市名*/ function get_my_addr_infos(){ $ch = curl_init(); $timeout = 5; $lat = $list['info' ...
- python地址解析经纬度,城市
1.地址列表 1.txt 上海市普陀区梅川路299-301号 浙江省杭州市拱墅区丰登路305-311号1层 江苏省南京市鼓楼区碧树园86号101室 浙江省宁波市江北区范江岸路38弄6号-10号1层商铺 ...
- php 获取客户端IP地址经纬度所在城市
1. [代码]获取客户端IP地址经纬度所在城市 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php $getIp=$_SERVER["REMOTE_ADDR ...
- 猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差 ...
- AJ学IOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
AJ分享,必须精品 一:效果 输入经纬度,可以得到相应的地名 二:思路 跟地里编码差不多 1.获取用户输入的经纬度 2.根据用户输入的经纬度创建CLLocation对象 3.根据CLLocation对 ...
- iOS之获取经纬度并通过反向地理编码获取详细地址
_locationManager = [[CLLocationManager alloc] init]; //期望的经度 _locationManager.desiredAccuracy = kCLL ...
- iOS 获取当前经纬度
一般说来LBS功能一般分为两块:一块是地理定位,就是获取当前精度.纬度和地理位置的功能,这一部分功能主要用到CoreLocation.Frameworks.一部分就是显示地图信息.丰富地图内容等,这一 ...
- 【Python】使用geocoder找出本机IP所在经纬度和城市
代码: import geocoder g = geocoder.ip('me') print(g.latlng) # 经纬度 print(g.city) # 所在城市 输出: C:\Users\ho ...
- 微信小程序获取经纬度所在城市
小程序的wx.getLocation()获得是经纬度并不包含地名,所以要通过经纬度用相应的地图转换出地名(本文使用的是百度地图) // 获取坐标 onLoad: function (options) ...
随机推荐
- Theano2.1.5-基础知识之打印出theano的图
来自:http://deeplearning.net/software/theano/tutorial/printing_drawing.html Printing/Drawing Theano gr ...
- BatsingJSLib 2.3、Ajax上传多个文件
//2.3Ajax上传单个或多个文件 //<input type="file" multiple="multiple"/> //参数:文件的表单JD ...
- favicon.ico文件简介
本地调试时,控制台经常会打印如下的错误(对 favicon.ico 的请求在 chrome 调试面板下不可见,可在抓包工具,比如 Fiddler 中看到): favicon.ico 是啥?看下面这张图 ...
- Python 处理数据库返回结果
游标执行后返回的结果都只是数据,但是不带有列名标识.这里需要处理2个问题: 将返回的数据映射到每一列上 当返回的结果很大的时候,需要使用迭代器来提升性能. 解决上面的2个问题,在python里面可以采 ...
- IT人员如何保护视力
最近感觉眼比较难受,有点干,估计是因为用上老婆淘汰的iPhone5C后屏幕太小,而我又是一个手机瘾重点患者的原因.为了保持自己5.0+的视力,做了以下工作,分享给各位朋友: Win7电脑将字体放大到1 ...
- 3n+1b 备忘录方法
题目详情 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数学家大会上公布了这个猜 ...
- 转:PHP中防止SQL注入的方法
[一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最 ...
- 创建Maven项目
在MyEclipse10中创建Maven Web项目 1.构建maven项目 2.将maven项目转换成Dynamic Web Project 3.设置部署集 4.pom.xml文件配置 参考: ht ...
- 传入任意json数据源进行格式化处理并用Angularjs显示
在项目开发中,有些时候我们并不知道传入的数据源(只针对于json格式的)的内部结构,本文用最笨的办法先把数据源格式化一遍,把所有的key值替换成自定义的值,然后在页面上用angularjs展示. ht ...
- bzoj1503
treap改了好长时间,erase写错了... #include<iostream> #include<cstdio> #include<cstdlib> usin ...