IOS定位 - 通俗定位(沒有地圖) - 反地輿編碼(獲得詳細地位),上面經由過程代碼給年夜家詳解,代碼以下:
#import <CoreLocation/CoreLocation.h> 應用到的頭文件 要引入CoreLocation這個包
<CLLocationManagerDelegate> 應用的署理稱號
//1.應用定位辦事
//設置app有拜訪定位辦事的權限
//在應用運用時代 / 一直(app在後台)
//info.plist文件添加以下兩條(或許個中一條):
//NSLocationWhenInUseUsageDescription 在應用運用時代
//NSLocationAlwaysUsageDescription 一直
//2.LocationManager 對象治理相干的定位辦事
_manager = [[CLLocationManager alloc] init];
//manager斷定: 手機能否開啟定位 / app能否有拜訪定位的權限
//[CLLocationManager locationServicesEnabled]; //手機能否開啟定位
//[CLLocationManager authorizationStatus]; //app拜訪定位的權限的狀況
if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
[_manager requestWhenInUseAuthorization]; //向用戶要求拜訪定位辦事的權限
}
_manager.delegate = self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
_manager.distanceFilter = 1.0f;
[_manager startUpdatingLocation];
//定位署理經緯度回調
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[_manager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSDictionary *test = [placemark addressDictionary];
// Country(國度) State(城市) SubLocality(區) Name全稱
NSLog(@"%@", [test objectForKey:@"Name"]);
}
}];
}
IOS百度地圖的應用(通俗定位、反地輿編碼)
1.起首接收根本的地圖功效
新建一個地圖類,xib拖也行,我這邊是代碼完成的。
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];//添加mapVIew
[self.view addSubview:_mapView];
#pragma mark - 設置mapView屬性
-(void)setMapViewProperty
{
_mapView.mapType = BMKUserTrackingModeFollowWithHeading;
_mapView.showsUserLocation = YES; //能否顯示定位圖層(即我的地位的小圓點)
_mapView.zoomLevel = 16;//地圖顯示比例
_mapView.rotateEnabled = NO; //設置能否可以扭轉
[self passLocationValue];
}
#pragma mark -傳入定位坐標
//設置定位到得用戶的地位,這裡是簡略的運用辦法(必需翻開法式時曾經獲得到地輿地位坐標,為懂得決地圖定位時老是先顯示天安門)
-(void)passLocationValue
{
BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake([UserLocationManager sharedInstance].clloction.coordinate, BMKCoordinateSpanMake(0.02f,0.02f));
BMKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
}
#pragma mark -設置定位圓點屬性
-(void)setUserImage
{
//用戶地位類
BMKLocationViewDisplayParam* param = [[BMKLocationViewDisplayParam alloc] init];
param.locationViewOffsetY = 0;//偏移量
param.locationViewOffsetX = 0;
param.isAccuracyCircleShow =NO;//設置能否顯示定位的誰人精度圈
param.isRotateAngleValid = NO;
[_mapView updateLocationViewWithParam:param];
}
如許根本的地圖界面就出來了
假如你須要在地圖上做一些要求,可以完成BMKMapViewDelegate,以下是mapView的一些協定辦法
**
*地圖區域行將轉變時會挪用此接口
*@param mapview 地圖View
*@param animated 能否動畫
*/
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
//TODO
}
/**
*地圖區域轉變完成後會挪用此接口
*@param mapview 地圖View
*@param animated 能否動畫
*/
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
//TODO
}
/**
*地圖狀況轉變完成後會挪用此接口
*@param mapview 地圖View
*/
- (void)mapStatusDidChanged:(BMKMapView *)mapView
{
//TODO
}
2.地圖定位
我這邊是將定位封裝了一個自力的manager類來治理定位和地圖上滑動到的地位,是將定位功效和地圖mapVIew自力開來,治理地輿挪動地位的變更
#import <Foundation/Foundation.h>
#import "BMapKit.h"
@interface UserLocationManager : NSObject <BMKMapViewDelegate,BMKLocationServiceDelegate>
{
CLLocation *cllocation;
BMKReverseGeoCodeOption *reverseGeoCodeOption;//逆地輿編碼
}
@property (strong,nonatomic) BMKLocationService *locService;
//城市名
@property (strong,nonatomic) NSString *cityName;
//用戶緯度
@property (nonatomic,assign) double userLatitude;
//用戶經度
@property (nonatomic,assign) double userLongitude;
//用戶地位
@property (strong,nonatomic) CLLocation *clloction;
//初始化單例
+ (UserLocationManager *)sharedInstance;
//初始化百度地圖用戶地位治理類
- (void)initBMKUserLocation;
//開端定位
-(void)startLocation;
//停滯定位
-(void)stopLocation;
@end
#import "UserLocationManager.h"
@implementation UserLocationManager
+ (UserLocationManager *)sharedInstance
{
static UserLocationManager *_instance = nil;
@synchronized (self) {
if (_instance == nil) {
_instance = [[self alloc] init];
}
}
return _instance;
}
-(id)init
{
if (self == [super init])
{
[self initBMKUserLocation];
}
return self;
}
#pragma 初始化百度地圖用戶地位治理類
/**
* 初始化百度地圖用戶地位治理類
*/
- (void)initBMKUserLocation
{
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
[self startLocation];
}
#pragma 翻開定位辦事
/**
* 翻開定位辦事
*/
-(void)startLocation
{
[_locService startUserLocationService];
}
#pragma 封閉定位辦事
/**
* 封閉定位辦事
*/
-(void)stopLocation
{
[_locService stopUserLocationService];
}
#pragma BMKLocationServiceDelegate
/**
*用戶地位更新後,會挪用此函數
*@param userLocation 新的用戶地位
*/
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation
{
cllocation = userLocation.location;
_clloction = cllocation;
_userLatitude = cllocation.coordinate.latitude;
_userLongitude = cllocation.coordinate.longitude;
[self stopLocation];(假如須要及時定位不消停滯定位辦事)
}
/**
*在停滯定位後,會挪用此函數
*/
- (void)didStopLocatingUser
{
;
}
/**
*定位掉敗後,會挪用此函數
*@param error 毛病號
*/
- (void)didFailToLocateUserWithError:(NSError *)error
{
[self stopLocation];
}
以上代碼就是本文IOS百度地圖的應用(通俗定位、反地輿編碼),願望對年夜家往後的任務和進修有所贊助。
【ios百度地圖的應用(通俗定位、反地輿編碼)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!