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路径下. ...
随机推荐
- ural-1099-Work Scheduling(裸带花树)
题意: 有N个人,有限对的人可以在一起工作,问最多能有多少对. 分析: 任意图的最大匹配 // File MAXName: 1099.cpp // Author: Zlbing // Created ...
- HNOI 2016 省队集训日记
第一天 DeepDarkFantasy 从东京出发,不久便到一处驿站,写道:日暮里. ——鲁迅<藤野先生> 定义一个置换的平方为对1~n的序列做两次该置换得到的序列.已知一个置换的平方, ...
- 【动态规划】Vijos P1121 马拦过河卒
题目链接: https://vijos.org/p/1616 题目大意: 卒从(0,0)走到(n,m),只能向下或向右,不能被马一步碰到或走到马,有几种走法. 题目思路: [动态规划] 把马控制的地方 ...
- Missing Number ——LeetCode
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- selenium python presence_of_element_located vs visibility_of_element_located
背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待元素出现.结果屡屡出问题.元素默认是隐藏的,导致等待过早的就结 ...
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
- zznu 1068: 进制转换
进制应该属于程序员的看家本事了,也是大家水平告别菜鸟的一个转折,所以进制转换题目是很有意义的, 这个题目是最简单的把二进制数化简成十进制,因为输入有可能有31位,所以无法使用int或者long lon ...
- 启动tomcat时报classpath not found
启动tomcat时报classpath not found 原因是缺包,首先查看tomcat安装地址,然后找到webapps目录下,找到该项目,看lib下是否缺包,不能单纯的看项目下是否缺包.
- sql给整数补零
update hs_user.clientorder a set a.stockcode = lpad(a.stockcode,6,'0') where a.market = 'SZ'
- sqlserver客户端连接只显示特定数据库的配置方法
首先,在实例级,有一个 view any database的这个属性,打开时可以看到所有数据库的元数据表,因此能看到实例下所有数据库的名字.默认public角色拥有这个属性.所以新建的登陆是可以看到所 ...