android经纬度获取行政区,获取当前经纬度、当前位置省市区(工具类)
//
// YALocationInfo.h
// YATestFramework
//
// Created by MacDev1 on 15/6/25.
// Copyright (c) 2015年 yanan. All rights reserved.
//
/*
* 需要导入 CoreLocation.framework
*/
#import
#import
@class YALocationInfor;
typedef NS_ENUM(NSInteger, LocationResult) {
FAIL = 0,
SUCCESS ,
};
typedef void(^LocBlock)(YALocationInfor *locInforModel, LocationResult locRes, NSError *error);
@interface YALocationInfor : NSObject
/**
*@briefYES 有定位的信息可以使用(可以自行决定是否再次调用 getUserCurrentLocationInfo 定位);NO 还没有进行定位或者定位尚未成功过
*/
@property (assign, nonatomic, readonly) BOOL isHaveLocInfor;
/**
*@brief用户当前的纬度
*/
@property (assign, nonatomic, readonly) CLLocationDegrees uLatitude;
/**
*@brief用户当前的经度
*/
@property (assign, nonatomic, readonly) CLLocationDegrees uLongitude;
/**
*@brief国家
*/
@property (strong, nonatomic, readonly) NSString *uCounty;
/**
*@brief国家码
*/
@property (strong, nonatomic, readonly) NSString *uCountryCode;
/**
*@brief省或直辖市
*/
@property (strong, nonatomic, readonly) NSString *uProvince;
/**
*@brief城市
*/
@property (strong, nonatomic, readonly) NSString *uCity;
/**
*@brief完整的详细信息
*/
@property (strong, nonatomic, readonly) NSString *uAddress;
/**
*@brief街道
*/
@property (strong, nonatomic, readonly) NSString *uStreet;
/**
*@brief区 e.g. 昌平区
*/
@property (strong, nonatomic, readonly) NSString *uSubLocality;
/**
*@brief门牌号
*/
@property (strong, nonatomic, readonly) NSString *uSubThoroughfare;
/**
*@brief路名
*/
@property (strong, nonatomic, readonly) NSString *uThoroughfare;
/**
*@brief地址详情
*/
@property (strong, nonatomic, readonly) NSString *uDetailLocString;
/**
*@brief单例
*
*@return
*/
+ (YALocationInfor *)shareLocationInforModel;
/**
*@brief定位(或重新定位)获取用户当前位置信息
*
*@return
*/
- (void)getUserCurrentLocationInfo:(LocBlock)LocationBlock;
/**
* 根据2个经纬度计算距离
*/
+(double) LantitudeLongitudeDist:(double)lon1 other_Lat:(double)lat1 self_Lon:(double)lon2 self_Lat:(double)lat2;
@end
//
// YALocationInfo.m
// YATestFramework
//
// Created by MacDev1 on 15/6/25.
// Copyright (c) 2015年 yanan. All rights reserved.
//
#import "YALocationInfor.h"
@interface YALocationInfor ()
@property (strong, nonatomic) CLLocationManager *mLocationManager;
@property (copy, nonatomic) LocBlock mLocationBlock;
@end
@implementation YALocationInfor
#pragma mark - 单例
static YALocationInfor *single = nil;
+ (YALocationInfor *)shareLocationInforModel {
@synchronized(self) {
if (!single) {
single = [[YALocationInfor alloc]init];
// 调用获取位置信息的方法
[single getUserCurrentLocationInfo:NULL];
}
return single;
}
}
#pragma mark - 开始获取用户当前的位置信息
- (void)getUserCurrentLocationInfo:(LocBlock)LocationBlock {
_mLocationBlock = LocationBlock;
if (!_mLocationManager) {
_mLocationManager = [[CLLocationManager alloc]init];
_mLocationManager.distanceFilter = kCLDistanceFilterNone; // meters
_mLocationManager.delegate = self;
_mLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
if (IS_IOS8_LATER) {
/* 需要在info中新增两个key NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription */
//使用期间
[_mLocationManager requestWhenInUseAuthorization];
//始终
// [self.locationManage requestAlwaysAuthorization]
}
}
if (_mLocationManager)
[_mLocationManager startUpdatingLocation];
}
#pragma mark - 成功获取定位数据后
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *currentLocation = [locations lastObject];
_uLatitude = currentLocation.coordinate.latitude;
_uLongitude = currentLocation.coordinate.longitude;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *array, NSError *error) {
if (array.count > 0) {
CLPlacemark *placemark = [array objectAtIndex:0];
[self locSuccessSet:placemark];
if (_mLocationBlock)
_mLocationBlock(self, SUCCESS, error);
} else if (error == nil && [array count] == 0) {
if (_mLocationBlock)
_mLocationBlock(self, FAIL, error);
} else if (error != nil) {
if (_mLocationBlock)
_mLocationBlock(self, FAIL, error);
}
}];
[_mLocationManager stopUpdatingLocation];
}
#pragma mark - 定位失败时回调
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
if (_mLocationBlock) {
_mLocationBlock(self, FAIL, error);
}
}
#pragma mark - 处理定位成功后,反向地理编码得到的信息
- (void)locSuccessSet:(CLPlacemark *)placemark {
_isHaveLocInfor = YES;
//获取城市
NSString *city = placemark.locality;
if (!city) {
// 四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
_uCity = city;
_uCounty = [placemark.addressDictionary objectForKey:@"Country"];
_uCountryCode = [placemark.addressDictionary objectForKey:@"CountryCode"];
_uProvince = [placemark.addressDictionary objectForKey:@"State"];
_uAddress = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
_uStreet = [placemark.addressDictionary objectForKey:@"Street"];
_uSubLocality = [placemark.addressDictionary objectForKey:@"SubLocality"];
_uSubThoroughfare = [placemark.addressDictionary objectForKey:@"SubThoroughfare"];
_uThoroughfare = [placemark.addressDictionary objectForKey:@"Thoroughfare"];
_uDetailLocString = [[NSString alloc]initWithFormat:@"%@%@%@",_uProvince?_uProvince:@"",_uSubLocality?_uSubLocality:@"",_uStreet?_uStreet:@""];
// FIXLog(@"\n\n\n\nplacemark.addressDictionary = %@\n\n\n\n",placemark.addressDictionary);
}
#pragma mark - iOS8 新增回调方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if ([_mLocationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_mLocationManager requestWhenInUseAuthorization];
}
break;
default:
break;
}
}
/**
* 根据2个经纬度计算距离
*/
//+(NSString *) LantitudeLongitudeDist:(double)lon1 other_Lat:(double)lat1 self_Lon:(double)lon2 self_Lat:(double)lat2
//{
//
// CLLocation *orig=[[CLLocation alloc] initWithLatitude:lat2 longitude:lon2] ;
// CLLocation* dist=[[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
//
// CLLocationDistance kilometers=[orig distanceFromLocation:dist] / 1000;
//
//
// return [NSString stringWithFormat:@"%0.2f",kilometers];
//}
#define PI 3.1415926
+(double) LantitudeLongitudeDist:(double)lon1 other_Lat:(double)lat1 self_Lon:(double)lon2 self_Lat:(double)lat2{
double er = 6378137; // 6378700.0f;
//ave. radius = 6371.315 (someone said more accurate is 6366.707)
//equatorial radius = 6378.388
//nautical mile = 1.15078
double radlat1 = PI*lat1/180.0f;
double radlat2 = PI*lat2/180.0f;
//now long.
double radlong1 = PI*lon1/180.0f;
double radlong2 = PI*lon2/180.0f;
if( radlat1 < 0 ) radlat1 = PI/2 + fabs(radlat1);// south
if( radlat1 > 0 ) radlat1 = PI/2 - fabs(radlat1);// north
if( radlong1 < 0 ) radlong1 = PI*2 - fabs(radlong1);//west
if( radlat2 < 0 ) radlat2 = PI/2 + fabs(radlat2);// south
if( radlat2 > 0 ) radlat2 = PI/2 - fabs(radlat2);// north
if( radlong2 < 0 ) radlong2 = PI*2 - fabs(radlong2);// west
//spherical coordinates x=r*cos(ag)sin(at), y=r*sin(ag)*sin(at), z=r*cos(at)
//zero ag is up so reverse lat
double x1 = er * cos(radlong1) * sin(radlat1);
double y1 = er * sin(radlong1) * sin(radlat1);
double z1 = er * cos(radlat1);
double x2 = er * cos(radlong2) * sin(radlat2);
double y2 = er * sin(radlong2) * sin(radlat2);
double z2 = er * cos(radlat2);
double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
//side, side, side, law of cosines and arccos
double theta = acos((er*er+er*er-d*d)/(2*er*er));
double dist = theta*er;
return dist / 1000;
}
@end
android经纬度获取行政区,获取当前经纬度、当前位置省市区(工具类)相关推荐
- Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习...
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- android 弹出编辑框,Android编程实现的EditText弹出打开和关闭工具类
本文实例讲述了Android编程实现的EditText弹出打开和关闭工具类.分享给大家供大家参考,具体如下: 需求: 使用代码实现Android的输入框EditText对键盘的关闭弹出的实现. 代码: ...
- Android基础入门教程——8.3.1 三个绘图工具类详解
Android基础入门教程--8.3.1 三个绘图工具类详解 标签(空格分隔): Android基础入门教程 本节引言: 上两小节我们学习了Drawable以及Bitmap,都是加载好图片的,而本节我 ...
- android获取操作系统版本号,Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类...
1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android系统版本号 5.获取手机IMEI串号 6.获取手机中的语言列表 SystemUtil类 1. /** 2. ...
- Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类...
最近在开发中,需要用到一些系统信息,这里我把这些方法写成一个工具类方便以后复用,该工具类有以下6个功能: 1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android ...
- Android 获取手机的厂商、型号、Android系统版本号、IMEI、当前系统语言等工具类
最近在开发中,需要用到一些系统信息,这里我把这些方法写成一个工具类方便以后复用,该工具类有以下6个功能: 1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android ...
- JAVA获取N个工作日后的时间的工具类、考虑上班时间、时区
DayWorkTime代表工作时间描述类 HolidayUtils是计算时间的工具类,addSecondByWorkDay用于计算时间加上指定秒后的工作时间,会自动跳过周末.节假日等.其中holida ...
- 解密android日志xlog,安卓开发技巧2:自定义日志工具类XLog的实现
安卓开发技巧二:自定义日志工具类XLog的实现 我们在开发过程中,打印日志是必不可少的一个调试环节,然而,直接使用系统自带的Log日志类,并不能满足我们实际项目的需求:假如我们现在在开发一款比较大的项 ...
- Android开发之dp转像素,像素转换为dp工具类,详细代码,带有源文件下载地址。...
2019独角兽企业重金招聘Python工程师标准>>> import android.content.Context; /** * @author 官网:http://www.93s ...
最新文章
- 从《我要投资》,看麓谷基金广场的“孤峰优势”
- python 从excel中抓取数据_使用Python抓取美团数据存于Excel中
- 从前中后序遍历构造二叉树,三题无脑秒杀
- c语言程序设计移动字母,C语言程序设计模拟试题二(含答案)
- 第七次spring会议
- Python-Flask实现电影系统管理后台
- dijkstra java pre_Dijkstra算法实现
- html5 input选择文件,input文件选择,限定文件类型。
- 【代码优化】考虑使用静态工厂方法代替构造器
- Xenu-死链接检测工具
- php微信公众号绑定微信号,订阅号实现微信网页授权登陆(原创)
- python暑假培训班
- [BZOJ4134][JZOJ4401]ljw和lzr的hack比赛
- [转贴]比《同居密友》更搞笑的【阿奴与唐玉】陶海风格
- TANRIC:肿瘤相关lncRNA数据库
- 和量子计算有什么区别 并发_到底什么是量子计算?
- Online Tools
- 第3周课件-全网最详细的ORB-SLAM2精讲
- CI持续集成系统环境---部署gerrit环境完整记录
- vgg16卷积层的计算量_卷积神经网络VGG16详解