iOS开发日记49-详解定位CLLocation
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS开发日记49-详解定位CLLocation,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2168字,纯文字阅读大概需要4分钟。
内容图文
![iOS开发日记49-详解定位CLLocation](/upload/InfoBanner/zyjiaocheng/1085/38fbc69d1ebd4cd689b1f4488e5d4eb6.jpg)
今天博主有一个定位CLLocation的需求,遇到了一些困难点,在此和大家分享,希望能够共同进步.
使用定位功能,首先要导入框架,遵守CLLocationManagerDelegate协议,再创建位置管理器CLLocationManager
在iOS8.0后,定位功能需要在info.plist中加入NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription这两个NSString类型字段,才能够使用定位功能
代码贴出来与大家共勉,各位看官自行研究
{
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
if([CLLocationManager locationServicesEnabled] == NO) {
// NSLog(@"没有GPS服务");
}
//地理位置精确度
_locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
//设置距离筛选器,double类型,只要距离变化多少,就调用委托代理
self.locationManager.distanceFilter = kCLDistanceFilterNone; // meters
[_locationManager requestWhenInUseAuthorization];// 前台定位
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@"longitude = %f", ((CLLocation *)[locations
lastObject]).coordinate.longitude);
NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).coordinate.latitude);
CGFloat longTI=((CLLocation *)[locations
lastObject]).coordinate.longitude;
CGFloat latTI=((CLLocation *)[locations lastObject]).coordinate.latitude;
//将经度显示到label上
_longitudeLabel.text = [NSString stringWithFormat:@"%f",longTI];
//将纬度现实到label上
_latitudeLabel.text = [NSString stringWithFormat:@"%f",latTI];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray *array, NSError *error)
{
if (array.count > 0)
{
CLPlacemark *placemark = [array objectAtIndex:0];
// //将获得的所有信息显示到label上
// self.location.text = placemark.name;
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
// NSLog(@"city = %@", city);
_cityName=city;
}
else if (error == nil && [array count] == 0)
{
// NSLog(@"No results were returned.");
}
else if (error != nil)
{
// NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
原文:http://www.cnblogs.com/Twisted-Fate/p/4999905.html
内容总结
以上是互联网集市为您收集整理的iOS开发日记49-详解定位CLLocation全部内容,希望文章能够帮你解决iOS开发日记49-详解定位CLLocation所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。