CoreLocation+MapKit系统定位(含坐标以及详细地址)
iOS8 之后出现一些新的配置
[self.manager requestWhenInUseAuthorization];
并且在info.plist文件中增加
NSLocationWhenInUseUsageDescription BOOL YES
NSLocationAlwaysUsageDescription string “提示描述”
记得加依赖库
CoreLocation.framework MapKit.framework
创建MapView
if (_mapView == nil) {
_mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 0.5, 0.5)];
// _mapView.userTrackingMode = MKUserTrackingModeFollow;
_mapView.delegate = self;
_mapView.showsUserLocation = YES;
[self.view addSubview:_mapView];
}
创建locationManager
if ([CLLocationManager locationServicesEnabled] == YES) {//判断定位是否可用
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;//定位精确度
_locationManager.distanceFilter = 10; //位置变化10米更新位置信息
//NSLog(@"定位");
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[_locationManager requestWhenInUseAuthorization];
}else{
//NSLog(@"定位不可用");
}
[_locationManager startUpdatingLocation];
CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//NSLog(@"%s",__FUNCTION__);
[manager stopUpdatingLocation];
NSLog(@"newLocation = %@",newLocation);
NSLog(@"oldLocation = %@",oldLocation);
//获取经度和纬度的结构体
CLLocationCoordinate2D coordinate = newLocation.coordinate;
//纬度信息,CLLocationDegrees类型实际上就是double型
CLLocationDegrees latitude = coordinate.latitude;
//经度信息,CLLocationDegrees类型实际上就是double型
CLLocationDegrees longitude = coordinate.longitude;
NSLog(@"%f,%f",latitude,longitude);
[self.latitude setText:[NSString stringWithFormat:@"纬度:%lf",latitude]];
[self.longitude setText:[NSString stringWithFormat:@"经度:%lf",longitude]];
/*
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
{
if (array.count > 0)
{
CLPlacemark *placemark = [array objectAtIndex:0];
//详细信息
[self.address setText:[NSString stringWithFormat:@"地址:%@",placemark.name]];
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
[self.city setText:[NSString stringWithFormat:@"城市:%@",city]];
}
else if (error == nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error != nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
*/
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"定位失败 ,%@",error);
}
MapView获得详细地址
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation * newLocation = userLocation.location;
self.lastCoordinate=mapView.userLocation.location.coordinate;
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
[standard setObject:@(self.lastCoordinate.longitude) forKey:MMLastLongitude];
[standard setObject:@(self.lastCoordinate.latitude) forKey:MMLastLatitude];
CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
CLGeocodeCompletionHandler handle = ^(NSArray *placemarks,NSError *error)
{
for (CLPlacemark * placeMark in placemarks)
{
NSDictionary *addressDic=placeMark.addressDictionary;
NSString *City=[addressDic objectForKey:@"City"];
// NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
// NSString * adressName = [NSString stringWithFormat:@"%@%@",subLocality,street];
[self.city setText:[NSString stringWithFormat:@"城市:%@",City]];
[self.address setText:[NSString stringWithFormat:@"地址:%@",placeMark.name]];
[self stopLocation];
}
};
[[NSUserDefaults standardUserDefaults] synchronize];
[clGeoCoder reverseGeocodeLocation:newLocation completionHandler:handle];
}
停止定位
-(void)stopLocation
{
if (_mapView) {
NSLog(@"关闭");
_mapView.showsUserLocation = NO;
[_mapView removeFromSuperview];
_mapView = nil;
}
}
来源: http://www.cnblogs.com/spaceID/p/4992167.html
CoreLocation+MapKit系统定位(含坐标以及详细地址)的更多相关文章
- 根据高德API知道坐标获取详细地址信息
/** * 根据坐标获取具体地址 * @param coor 坐标字符串 * @return */ public static String getAdd(String coor){ String u ...
- selenium使用location定位元素坐标偏差
python+selenium+Chromedriver使用location定位元素坐标偏差使用xpath定位元素,用.location获取坐标值,截取网页截图的一部分出现偏差. 之所以会出现这个坐标 ...
- 系统定位在iOS8中的改变
CLLocationManager这个系统定位的类在iOS8之前要实现定位,只需要遵守CLLocationManagerDelegate这个代理即可: - (void)startLocate { ...
- [课程设计]Scrum 3.4 多鱼点餐系统开发进度(下单详细信息页面&会员信息页面)
Scrum 3.4 多鱼点餐系统开发进度(下单详细信息页面&会员信息页面) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队 ...
- [课程设计]Scrum 3.3 多鱼点餐系统开发进度(下单详细信息页面设计)
Scrum 3.3 多鱼点餐系统开发进度(下单详细信息页面设计) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点 ...
- 【Oracle RAC】Linux系统Oracle11gR2 RAC安装配置详细过程V3.1(图文并茂)
[Oracle RAC]Linux系统Oracle11gR2 RAC安装配置详细过程V3.1(图文并茂) 2 Oracle11gR2 RAC数据库安装准备工作2.1 安装环境介绍2.2 数据库安装软件 ...
- 如何设置Linux(Centos)系统定期任务(corntab详细用法)
如何设置Linux(Centos)系统定期任务(crontab详细用法) 1.Crontab简介 Linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非 ...
- 【Oracle RAC】Linux系统Oracle12c RAC安装配置详细记录过程V2.0(图文并茂)
[Oracle RAC]Linux系统Oracle12c RAC安装配置详细过程V2.0(图文并茂) 2 Oracle12c RAC数据库安装准备工作2.1 安装环境介绍2.2 数据库安装软件下载3 ...
- Deepin系统手动安装oracle jdk8详细教程
Deepin系统手动安装oracle jdk8详细教程 oracle官网下载jdk压缩包,使用 sudo tar -zxf jdk***解压文件,我放在在了home/diy/java/jdk路径下. ...
随机推荐
- UpdateLayeredWindow, Layered Windows, codeproject
http://www.codeproject.com/Articles/16362/Bring-your-frame-window-a-shadow http://www.codeproject.co ...
- angularjs学习总结(~~很详细的教程)
1 前言 前端技术的发展是如此之快,各种优秀技术.优秀框架的出现简直让人目不暇接,紧跟时代潮流,学习掌握新知识自然是不敢怠慢. AngularJS是google在维护,其在国外已经十分火热,可是国内的 ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...
- Ubuntu 14.04 Remmina远程桌面连接Windows计算机
可以在/usr/share/applications/目录下启动Remmina远程桌面客户端或者直接在Dash中搜索Remmina即可 启动后如下图,可以新建,复制,编辑,删除远程桌面客户端 选择新建 ...
- 线性规划||网络流(费用流):COGS 288. [NOI2008] 志愿者招募
[NOI2008] 志愿者招募 输入文件:employee.in 输出文件:employee.out 简单对比 时间限制:2 s 内存限制:512 MB [问题描述] 申奥成功后,布布经过 ...
- IOI 2009:Mecho
IOI2009 Mecho Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Origin ...
- HDOJ 2071 Max Num
Problem Description There are some students in a class, Can you help teacher find the highest studen ...
- [已解决问题] Could not find class XXX referenced from method XXX.<YYY>
导入Jar包的问题,有时候即使引入了Jar包也会报错,比如我在引入了libsvm.jar后仍然会报此错 解决方法是: Step 1. 创建User library,随便命一个名,然后把Jar包导入 S ...
- cannot be deleted directly via the port API: has device owner network:floatingip
- I - Arbitrage
题目大意:套汇 套利是使用货币汇率的差异将一个单位的货币转换为多个相同的货币单位,例如1美元可以买0.5英镑,1英镑可以买10法郎,1法郎可以买0.21美元,然后聪明的人经过一些列兑换可以得到 1*0 ...