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路径下. ...
随机推荐
- Strust2的json插件
以下这段摘自网上: Json是一种轻量级的数据交换格式,JSon插件提供了一种名为json的ActionResultType .一旦为Action指定了该结果处理类型,JSON插件就会自动将Actio ...
- hibernate 3.* C3P0配置 以及为什么需要连接池!
Hibernate自带的连接池算法相当不成熟. 它只是为了让你快些上手,并不适合用于产品系统或性能测试中. 出于最佳性能和稳定性考虑你应该使用第三方的连接池.只需要用特定连接池的设置替换 hibern ...
- java学习之负数的进制
在上一篇内容当中说明了什么是进制,进制存在的原因以及形式,接下来说一下一个比较特殊的进制计算,就是负数的在计算机当中的表示.举个个例子来说 6的二进制表示是什么? 0000-0000-0000-000 ...
- 嵌入式ARM-Linux开发工具下载地址合集
insight gdb http://ftp.twaren.net/Unix/Sourceware/ 说明:GDB可视化调试工具 http://gro.clinux.org/frs/?group_i ...
- Merge Intervals——LeetCode
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- The game of life(生命游戏)新算法
我写了一种常见的实现算法,和另一种新算法,即不是每次循环计算每个细胞的周围细胞数来产生下一时刻,而是每次每个产生状态变化的细胞主动通知周围的邻居,因此每个细胞增加一个用来记录邻居数的字段.由邻居数决定 ...
- JavaScript 操作 DOM 常用 API 总结
文本整理了javascript操作DOM的一些常用的api,根据其作用整理成为创建,修改,查询等多种类型的api,主要用于复习基础知识,加深对原生js的认识. 基本概念 在讲解操作DOM的api之前, ...
- Xcode use Protocol buffer
http://stackoverflow.com/questions/10277576/google-protocol-buffers-on-ios http://stackoverflow.com/ ...
- openfire for mac 无法启动
http://blog.csdn.net/winer888/article/details/49886281 ①:sudo chmod -R 777 /usr/local/openfire/bin ② ...
- TestNG关键字和testNG.xml结构学习
转自官网:http://testng.org/doc/documentation-main.html#test-results TestNG关键字 @BeforeSuite@AfterSuite@Be ...