【iOS定位功能】ios城市定位功能实现
小序:
本文包括三个内容:
①:定位功能的实现
②:知道城市名拿到经纬度
③:根据经纬度拿到日出日落时间
关于定位我们要添加 #import <CoreLocation/CoreLocation.h>
头文件
一、定位功能
先创建这两个对象(这里是全局私有):
CLLocationManager* _manager //定位管理 CLGeocoder *_geocoder //地理编码
下面直接上代码了
//定位
//1.创建定位管理对象
_manager=[[CLLocationManager alloc]init];
_geocoder=[[CLGeocoder alloc]init];
//2.设置属性 distanceFilter、desiredAccuracy
_manager.distanceFilter=kCLDistanceFilterNone;//实时更新定位位置
_manager.desiredAccuracy=kCLLocationAccuracyBest;//定位精确度
if([_manager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[_manager requestAlwaysAuthorization];
}
//该模式是抵抗程序在后台被杀,申明不能够被暂停
_manager.pausesLocationUpdatesAutomatically=NO;
//3.设置代理 我们要遵循代理:<CLLocationManagerDelegate>
_manager.delegate=self;
//4.开始定位
[_manager startUpdatingLocation];
//5.获取朝向
[_manager startUpdatingHeading];
下面是代理方法
#pragma mark -- delegate
//定位失败时调用的方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError* )error
{
NSLog(@"%@",error);
}
//定位成功调用的的方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray* )locations
{
if(locations.count>0)
{
// 获取位置信息
CLLocation *loc=[locations lastObject];
// 获取经纬度的结构体
CLLocationCoordinate2D coor=loc.coordinate;
CLLocation *location=[[CLLocation alloc]initWithLatitude:coor.latitude longitude:coor.longitude];
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* placemarks, NSError *error) {
CLPlacemark *pmark=[placemarks firstObject];
/*//地址字典 这里都是关于位置的一些属性 我们拿到我们需要的信息 最后我打印的是一个完整的信息
NSLog(@"%@",pmark.addressDictionary);
位置信息:经纬度
NSString *city=pmark.location;
拿到城市 直辖市拿不到
NSString *city=pmark.locality;
拿到所在区
NSString *city=pmark.subLocality;
拿到所造号
NSString *city=pmark.subThoroughfare;
多少路多少号
NSLog(@"%@",pmark.name);
所在省份
NSString *city=pmark.administrativeArea;
*/
NSString *city=pmark.addressDictionary[@"City"];
// if([city hasSuffix:@"市辖区"])
// city=[city substringToIndex:city.length-3];
// if([city hasSuffix:@"市"])
// city=[city substringToIndex:city.length-1];
NSLog(@"城市:%@",city);
for (CLPlacemark * placemark in placemarks) {
NSDictionary *test = [placemark addressDictionary];
// Country(国家) State(省份) City(城市) SubLocality(区)
NSLog(@"%@", [[test objectForKey:@"Country"] stringByAppendingFormat:[NSString stringWithFormat:@"%@%@%@%@",[test objectForKey:@"State"], [test objectForKey:@"City"],[test objectForKey:@"SubLocality"],[test objectForKey:@"Street"]]]);
}
}];
}
}
二、根据城市名拿到经纬度
还是直接上代码
//根据城市拿到经纬度
NSString *oreillyAddress = @"beijing";
CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
[myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0 && error == nil) {
NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
CLPlacemark *firstPlacemark;
firstPlacemark = [placemarks objectAtIndex:0];
NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);//经度
NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);//纬度
三、根据经纬度算日出日落
就用上面那个方法拿到经纬度,算日出日落有一套算法 ,我这里也是用别人写好的类。说要的话跟我说,这里我就不贴了。展示下使用的代码
//根据经纬度判断
double latitude = firstPlacemark.location.coordinate.latitude;
double longitude = firstPlacemark.location.coordinate.longitude;
NSDate * date=[NSDate date];
//NSLog(@"当前时间%@",[self.formatter stringFromDate:date]);
NSDate* sunriseDate = RMSunCalculateSunrise(latitude, longitude, date, RMSunZenithOfficial);
if(sunriseDate == nil)
{
// This can occur in the extremes of the upper and lower hemispheres at certain times of year
NSLog(@"今天没有日出");
}
else
{
NSLog(@"日出:%@",[self.formatter stringFromDate:sunriseDate]);
}
NSDate* sunsetDate = RMSunCalculateSunset(latitude, longitude, date, RMSunZenithOfficial);
if(sunsetDate == nil)
{
// This can occur in the extremes of the upper and lower hemispheres at certain times of year
NSLog(@"今天没有日落") ;
}
else
{
NSLog(@"日落:%@",[self.formatter stringFromDate:sunsetDate]);
}
有什么问题可以一起交流哈。。
转载请注明:【iOS定位功能】ios城市定位功能实现 - 编程知识库
您可能还会对这些文章感兴趣
2016-12-23 64次iOS 码农的微信小程序开发总结
最近公司需要一个微信小程序demo去给客户看,就研究了几天。 下面就说说开发微信小程序的时候遇到的一些问题,对于小程序的开发教程就不细说了,很多人都写的比我好。下面就说一些我个人觉得比较实用的。 快速创建页面 刚开始开发小程序的时候,一个个创建对应页面的...
2016-12-23 118次微信小程序0.11.122100版本新功能解析
微信小程序0.11.122100版本新功能解析 时间 2016-12-22 08:15:21 小楼昨夜又秋风 相似文章 (1) 原文 https://zhuanlan.zhihu.com/p/24498136 主题 微信小程序开发 iOS开发 新版本就不再吐槽了,整的自己跟个愤青似的。人老了,喷不动了,把机会留给年轻人吧。...
2016-12-23 138次iOS后台定位并上传地理信息(滴滴打车类项目需求)
说起后台定位我们的说下以前iOS的那点事,在很早的版本里,后台运行程序不被iOS系统所认可,为啥呢,一个是耗电,一个违背了iOS运行的快照机制,所以说在以前程序置于后台后只有5秒的可操作时间,当然我们也可以申请去 加时间,但是系统只允许最大为10分钟的后台干活...
2016-12-23 43次【iOS定位功能】ios城市定位功能实现
小序: 本文包括三个内容: ①:定位功能的实现 ②:知道城市名拿到经纬度 ③:根据经纬度拿到日出日落时间 关于定位我们要添加 #import <CoreLocation/CoreLocation.h> 头文件 一、定位功能 先创建这两个对象(这里是全局私有): CLLocationManager* _manager //...
大家正在看
- 二类电商是什么意思? 二类电商有哪些?暴利二类电商还好做吗?
- 【二类电商广点通投放指南】二类电商广点通投放值不值
- 密码保护:支付宝突破微信封锁唤起支付宝代码
- Host is not allowed to connect to this MySQL server解决方法
- 密码保护:移动端js自动复制代码
- linux数据库调优,WordPress MySQL占用cpu高数据库优化
- 2017 年十大网页设计趋势
- 网页端的VR实现离我们还远么?
- 最完整的Chrome浏览器客户端调试大全
- iPhone用户人均每天遭电话骚扰1次
- 3G电子化销售服务系统
- Java WeakReference的理解与使用
- 搞清楚 Python traceback
- Referrer 还是 Referer?