iOS反地理编码,lbs定位
2016-06-15 10:52
411 查看
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MainTabBarViewController.h"
#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
@interface AppDelegate :
UIResponder <UIApplicationDelegate,UIActionSheetDelegate,CLLocationManagerDelegate>
@property (nonatomic,
assign) double longitude;//经度
@property (nonatomic,
assign) double latitude;//纬度
@property (strong,
nonatomic) UIWindow *window;
#import "AppDelegate.h"
@interface
AppDelegate (){
//定位
CLLocationManager *_locationManager;//用于获取位置
CLLocation *_checkLocation;//用于保存位置信息
}
@end
@implementation AppDelegate
-(void)initMapLatToLong{
_latitude =
LATITUDE_DEFAULT;
_longitude =
LONGITUDE_DEFAULT;
_locationManager = [[CLLocationManager
alloc] init];
if ([CLLocationManager
locationServicesEnabled]) {
NSLog(@"开始定位");
_locationManager.delegate =
self;
// distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
//
它的单位是米,这里设置为至少移动1000再通知委托处理更新;
_locationManager.distanceFilter = 200.0;
// kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
_locationManager.desiredAccuracy =
kCLLocationAccuracyBest;
//ios8+以上要授权,并且在plist文件中添加NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,值可以为空
if (IOS8 >=8.0) {//ios8+,不加这个则不会弹框
[_locationManager
requestWhenInUseAuthorization];//使用中授权
[_locationManager
requestAlwaysAuthorization];
}
[_locationManager
startUpdatingLocation];
}else{
NSLog(@"定位失败,请确定是否开启定位功能");
// _locationManager.delegate = self;
// // distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
// //
它的单位是米,这里设置为至少移动1000再通知委托处理更新;
// _locationManager.distanceFilter = 200.0;
// // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
// _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// [_locationManager startUpdatingLocation];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
[self
initMapLatToLong];
}
#pragma mark - CLLocationManagerDelegate
//舍弃了
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"didUpdateToLocation----");
_checkLocation = newLocation;
}
//ios 6.0sdk以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations{
NSLog(@"didUpdateToLocation+++");
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *cl = [locations
lastObject];
_latitude = cl.coordinate.latitude;
_longitude = cl.coordinate.longitude;
NSLog(@"纬度--%f",_latitude);
NSLog(@"经度--%f",_longitude);
//
获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder
alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:cl
completionHandler:^(NSArray *array,
NSError *error){
if (array.count > 0){
CLPlacemark *placemark = [array
objectAtIndex:0];
//将获得的所有信息
NSString *loction = placemark.name;
DDLogInfo(@"loction====%@",loction);
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSLog(@"city = %@", city);
NSString *cityText = city;
// [_cityButton setTitle:city forState:UIControlStateNormal];
DDLogInfo(@"cityText======%@",cityText);
}
else if (error ==
nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error !=
nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError
*)error{
NSLog(@"定位失败");
}
#import <CoreLocation/CoreLocation.h>
#import "MainTabBarViewController.h"
#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
@interface AppDelegate :
UIResponder <UIApplicationDelegate,UIActionSheetDelegate,CLLocationManagerDelegate>
@property (nonatomic,
assign) double longitude;//经度
@property (nonatomic,
assign) double latitude;//纬度
@property (strong,
nonatomic) UIWindow *window;
#import "AppDelegate.h"
@interface
AppDelegate (){
//定位
CLLocationManager *_locationManager;//用于获取位置
CLLocation *_checkLocation;//用于保存位置信息
}
@end
@implementation AppDelegate
-(void)initMapLatToLong{
_latitude =
LATITUDE_DEFAULT;
_longitude =
LONGITUDE_DEFAULT;
_locationManager = [[CLLocationManager
alloc] init];
if ([CLLocationManager
locationServicesEnabled]) {
NSLog(@"开始定位");
_locationManager.delegate =
self;
// distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
//
它的单位是米,这里设置为至少移动1000再通知委托处理更新;
_locationManager.distanceFilter = 200.0;
// kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
_locationManager.desiredAccuracy =
kCLLocationAccuracyBest;
//ios8+以上要授权,并且在plist文件中添加NSLocationWhenInUseUsageDescription,NSLocationAlwaysUsageDescription,值可以为空
if (IOS8 >=8.0) {//ios8+,不加这个则不会弹框
[_locationManager
requestWhenInUseAuthorization];//使用中授权
[_locationManager
requestAlwaysAuthorization];
}
[_locationManager
startUpdatingLocation];
}else{
NSLog(@"定位失败,请确定是否开启定位功能");
// _locationManager.delegate = self;
// // distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
// //
它的单位是米,这里设置为至少移动1000再通知委托处理更新;
// _locationManager.distanceFilter = 200.0;
// // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
// _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// [_locationManager startUpdatingLocation];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch.
[self
initMapLatToLong];
}
#pragma mark - CLLocationManagerDelegate
//舍弃了
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"didUpdateToLocation----");
_checkLocation = newLocation;
}
//ios 6.0sdk以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations{
NSLog(@"didUpdateToLocation+++");
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *cl = [locations
lastObject];
_latitude = cl.coordinate.latitude;
_longitude = cl.coordinate.longitude;
NSLog(@"纬度--%f",_latitude);
NSLog(@"经度--%f",_longitude);
//
获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder
alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:cl
completionHandler:^(NSArray *array,
NSError *error){
if (array.count > 0){
CLPlacemark *placemark = [array
objectAtIndex:0];
//将获得的所有信息
NSString *loction = placemark.name;
DDLogInfo(@"loction====%@",loction);
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSLog(@"city = %@", city);
NSString *cityText = city;
// [_cityButton setTitle:city forState:UIControlStateNormal];
DDLogInfo(@"cityText======%@",cityText);
}
else if (error ==
nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error !=
nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError
*)error{
NSLog(@"定位失败");
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- LBS BLOG在IE7下图片出现滚动条的解决方法
- LBS blog sql注射漏洞[All version]-官方已有补丁
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结