iOS中利用系统地图获取定位信息(经纬度/地理位置)
来源:互联网 发布:数据库事务案例 编辑:程序博客网 时间:2024/04/29 05:50
首先在plist文件中
但是对于8.0之后的9.0之间的版本有时在设置中更改定位权限会闪退 改成下面的就好啦
然后在.h文件中:
//
// CCLocationManager.h
// MMLocationManager
//
//
// Created by GuoFeng on 16/4/14.
// Copyright © 2016年 WXDL. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#define CCLastLongitude @"CCLastLongitude"
#define CCLastLatitude @"CCLastLatitude"
#define CCLastCity @"CCLastCity"
#define CCLastAddress @"CCLastAddress"
typedef void (^LocationBlock)(CLLocationCoordinate2D locationCorrrdinate);
typedef void (^LocationErrorBlock) (NSError *error);
typedef void(^NSStringBlock)(NSString *cityString);
typedef void(^NSStringBlock)(NSString *addressString);
@interface CCLocationManager :NSObject<CLLocationManagerDelegate>
@property (nonatomic)CLLocationCoordinate2D lastCoordinate;
@property(nonatomic,strong)NSString *lastCity;
@property (nonatomic,strong)NSString *lastAddress;
@property(nonatomic,assign)float latitude;
@property(nonatomic,assign)float longitude;
+ (CCLocationManager *)shareLocation;
/**
* 获取坐标
*
* @param locaiontBlock locaiontBlock description
*/
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock ;
/**
* 获取坐标和详细地址
*
* @param locaiontBlock locaiontBlock description
* @param addressBlock addressBlock description
*/
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock withAddress:(NSStringBlock) addressBlock;
/**
* 获取详细地址
*
* @param addressBlock addressBlock description
*/
- (void) getAddress:(NSStringBlock)addressBlock;
/**
* 获取城市
*
* @param cityBlock cityBlock description
*/
- (void) getCity:(NSStringBlock)cityBlock;
///**
// * 获取城市和定位失败
// *
// * @param cityBlock cityBlock description
// * @param errorBlock errorBlock description
// */
//- (void) getCity:(NSStringBlock)cityBlock error:(LocationErrorBlock) errorBlock;
@end
//
// CCLocationManager.m
// MMLocationManager
//
//
// Created by GuoFeng on 16/4/14.
// Copyright © 2016年 WXDL. All rights reserved.
//
#import "CCLocationManager.h"
@interface CCLocationManager (){
CLLocationManager *_manager;
}
@property (nonatomic,strong)LocationBlock locationBlock;
@property (nonatomic,strong)NSStringBlock cityBlock;
@property (nonatomic,strong)NSStringBlock addressBlock;
@property (nonatomic,strong)LocationErrorBlock errorBlock;
@end
@implementation CCLocationManager
+ (CCLocationManager *)shareLocation{
staticdispatch_once_t pred =0;
__strongstaticid _sharedObject =nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id)init {
self = [super init];
if (self) {
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
float longitude = [standard floatForKey:CCLastLongitude];
float latitude = [standard floatForKey:CCLastLatitude];
self.longitude = longitude;
self.latitude = latitude;
self.lastCoordinate = CLLocationCoordinate2DMake(longitude,latitude);
self.lastCity = [standard objectForKey:CCLastCity];
self.lastAddress=[standard objectForKey:CCLastAddress];
}
returnself;
}
//获取经纬度
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock
{
self.locationBlock = [locaiontBlock copy];
[self startLocation];
}
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock withAddress:(NSStringBlock) addressBlock
{
self.locationBlock = [locaiontBlock copy];
self.addressBlock = [addressBlock copy];
[self startLocation];
}
- (void) getAddress:(NSStringBlock)addressBlock
{
self.addressBlock = [addressBlock copy];
[self startLocation];
}
//获取省市
- (void) getCity:(NSStringBlock)cityBlock
{
self.cityBlock = [cityBlock copy];
[self startLocation];
}
//- (void) getCity:(NSStringBlock)cityBlock error:(LocationErrorBlock) errorBlock
//{
// self.cityBlock = [cityBlock copy];
// self.errorBlock = [errorBlock copy];
// [self startLocation];
//}
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error)
{
if (placemarks.count >0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
_lastCity = [NSString stringWithFormat:@"%@%@",placemark.administrativeArea,placemark.locality];
[standard setObject:_lastCity forKey:CCLastCity];//省市地址
NSLog(@"______%@",_lastCity);
_lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare];//详细地址
if (placemark.subThoroughfare.length==0) {
_lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare];
}
NSLog(@"______%@",_lastAddress);
}
if (_cityBlock) {
_cityBlock(_lastCity);
_cityBlock = nil;
}
if (_addressBlock) {
_addressBlock(_lastAddress);
_addressBlock = nil;
}
}];
_lastCoordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude ,newLocation.coordinate.longitude );
if (_locationBlock) {
_locationBlock(_lastCoordinate);
_locationBlock = nil;
}
NSLog(@"%f--%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
[standard setObject:@(newLocation.coordinate.latitude) forKey:CCLastLatitude];
[standard setObject:@(newLocation.coordinate.longitude) forKey:CCLastLongitude];
[manager stopUpdatingLocation];
}
-(void)startLocation
{
if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
_manager=[[CLLocationManager alloc]init];
_manager.delegate=self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
[_manager requestAlwaysAuthorization];
_manager.distanceFilter=100;
[_manager startUpdatingLocation];
}
else
{
UIAlertView *alvertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"需要开启定位服务,请到设置->隐私,打开定位服务" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alvertView show];
}
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
[self stopLocation];
}
-(void)stopLocation
{
_manager = nil;
}
@end
#import "CCLocationManager.h"
在需要用的地方:
-(void)addressInfo
{
if (IS_IOS8) {
[UIApplicationsharedApplication].idleTimerDisabled =TRUE;
locationmanager = [[CLLocationManageralloc]init];
[locationmanagerrequestAlwaysAuthorization]; //NSLocationAlwaysUsageDescription
[locationmanagerrequestWhenInUseAuthorization]; //NSLocationWhenInUseDescription
locationmanager.delegate =self;
}
//__block __weak MainTabbarController *wself = self;
__blockNSString *string;
if (IS_IOS8) {
[[CCLocationManagershareLocation]getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {
string = [NSStringstringWithFormat:@"%f %f",locationCorrrdinate.latitude,locationCorrrdinate.longitude];
_lat=[NSStringstringWithFormat:@"%f",locationCorrrdinate.latitude];
_long=[NSStringstringWithFormat:@"%f",locationCorrrdinate.longitude];
} withAddress:^(NSString *addressString) {
NSLog(@"+++++++++%@",addressString);
string = [NSStringstringWithFormat:@"%@\n%@",string,addressString];
// [wself setLabelText:string];
_addressinfo=addressString;
}
}];
}
}
- iOS中利用系统地图获取定位信息(经纬度/地理位置)
- 利用HTML5定位功能获取经纬度,在根据经纬度利用百度地图获取位置信息
- ios 定位经纬度获取位置信息,地理位置编码与反编码
- iOS - (利用/调用系统定位获取当前经纬度与地理信息)
- iOS上地图通过地理位置经纬度找出城市等信息
- iOS百度地图 定位,获取当前经纬度
- 通过经纬度获取地理位置信息
- android下利用高德地图获取经纬度等定位信息
- IOS 经纬度 转换地理位置信息
- js 地理位置查询经纬度定位地图
- Android中获取地理位置经纬度
- 地区经纬度信息获取(利用Google地图API获取)
- 地区经纬度信息获取(利用Google地图API获取)
- ios获取地理位置信息
- HTML5获取地理位置定位信息
- HTML5获取地理位置定位信息
- Android 通过经纬度获取地理位置信息
- Android 通过经纬度获取地理位置信息
- Mysql使用SQL的安全问题,Mysql防止SQL注入
- Linux的基本知识(1)
- 基于FL2440的QT5.6.0移植记录
- 怎么实现Automator中文件夹操作的应用
- 多按钮的事件相应,getActionCommand & setActionCommand篇
- iOS中利用系统地图获取定位信息(经纬度/地理位置)
- 程序进入后台运行NSTimer
- VS2010的Ribbon怎样调用透明背景的图标
- spring 声明式事务管理在真实的Service和单元测试时的回滚情况,需要注意的问题,jpa为例子
- Mysql SQL Mode详解
- 计算机网络杂学
- Python NLTK学习
- 汉字数字 转换int
- 几何画板怎么构造斜线阴影