ios百度地圖的使用(普通定位、反地理編碼)
iOS定位 - 普通定位(沒(méi)有地圖) - 反地理編碼(得到具體位置),下面通過(guò)代碼給大家詳解,代碼如下:
#import <CoreLocation/CoreLocation.h> 使用到的頭文件 要引入CoreLocation這個(gè)包
<CLLocationManagerDelegate> 使用的代理名稱
//1.使用定位服務(wù)
//設(shè)置app有訪問(wèn)定位服務(wù)的權(quán)限
//在使用應(yīng)用期間 / 始終(app在后臺(tái))
//info.plist文件添加以下兩條(或者其中一條):
//NSLocationWhenInUseUsageDescription 在使用應(yīng)用期間
//NSLocationAlwaysUsageDescription 始終
//2.LocationManager 對(duì)象管理相關(guān)的定位服務(wù)
_manager = [[CLLocationManager alloc] init];
//manager判斷: 手機(jī)是否開(kāi)啟定位 / app是否有訪問(wèn)定位的權(quán)限
//[CLLocationManager locationServicesEnabled]; //手機(jī)是否開(kāi)啟定位
//[CLLocationManager authorizationStatus]; //app訪問(wèn)定位的權(quán)限的狀態(tài)
if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
[_manager requestWhenInUseAuthorization]; //向用戶請(qǐng)求訪問(wèn)定位服務(wù)的權(quán)限
}
_manager.delegate = self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
_manager.distanceFilter = 1.0f;
[_manager startUpdatingLocation];
//定位代理經(jīng)緯度回調(diào)
-(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(國(guó)家) State(城市) SubLocality(區(qū)) Name全稱
NSLog(@"%@", [test objectForKey:@"Name"]);
}
}];
}
ios百度地圖的使用(普通定位、反地理編碼)
1.首先接受基本的地圖功能
新建一個(gè)地圖類,xib拖也行,我這邊是代碼實(shí)現(xiàn)的。
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];//添加mapVIew
[self.view addSubview:_mapView];
#pragma mark - 設(shè)置mapView屬性
-(void)setMapViewProperty
{
_mapView.mapType = BMKUserTrackingModeFollowWithHeading;
_mapView.showsUserLocation = YES; //是否顯示定位圖層(即我的位置的小圓點(diǎn))
_mapView.zoomLevel = 16;//地圖顯示比例
_mapView.rotateEnabled = NO; //設(shè)置是否可以旋轉(zhuǎn)
[self passLocationValue];
}
#pragma mark -傳入定位坐標(biāo)
//設(shè)置定位到得用戶的位置,這里是簡(jiǎn)單的應(yīng)用方法(必須打開(kāi)程序時(shí)已經(jīng)獲取到地理位置坐標(biāo),為了解決地圖定位時(shí)總是先顯示天安門)
-(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 -設(shè)置定位圓點(diǎn)屬性
-(void)setUserImage
{
//用戶位置類
BMKLocationViewDisplayParam* param = [[BMKLocationViewDisplayParam alloc] init];
param.locationViewOffsetY = 0;//偏移量
param.locationViewOffsetX = 0;
param.isAccuracyCircleShow =NO;//設(shè)置是否顯示定位的那個(gè)精度圈
param.isRotateAngleValid = NO;
[_mapView updateLocationViewWithParam:param];
}
這樣基本的地圖界面就出來(lái)了
如果你需要在地圖上做一些請(qǐng)求,可以實(shí)現(xiàn)BMKMapViewDelegate,以下是mapView的一些協(xié)議方法
**
*地圖區(qū)域即將改變時(shí)會(huì)調(diào)用此接口
*@param mapview 地圖View
*@param animated 是否動(dòng)畫(huà)
*/
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
//TODO
}
/**
*地圖區(qū)域改變完成后會(huì)調(diào)用此接口
*@param mapview 地圖View
*@param animated 是否動(dòng)畫(huà)
*/
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
//TODO
}
/**
*地圖狀態(tài)改變完成后會(huì)調(diào)用此接口
*@param mapview 地圖View
*/
- (void)mapStatusDidChanged:(BMKMapView *)mapView
{
//TODO
}
2.地圖定位
我這邊是將定位封裝了一個(gè)獨(dú)立的manager類來(lái)管理定位和地圖上滑動(dòng)到的位置,是將定位功能和地圖mapVIew獨(dú)立開(kāi)來(lái),管理地理移動(dòng)位置的變化
#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;
//用戶經(jīng)度
@property (nonatomic,assign) double userLongitude;
//用戶位置
@property (strong,nonatomic) CLLocation *clloction;
//初始化單例
+ (UserLocationManager *)sharedInstance;
//初始化百度地圖用戶位置管理類
- (void)initBMKUserLocation;
//開(kāi)始定位
-(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 打開(kāi)定位服務(wù)
/**
* 打開(kāi)定位服務(wù)
*/
-(void)startLocation
{
[_locService startUserLocationService];
}
#pragma 關(guān)閉定位服務(wù)
/**
* 關(guān)閉定位服務(wù)
*/
-(void)stopLocation
{
[_locService stopUserLocationService];
}
#pragma BMKLocationServiceDelegate
/**
*用戶位置更新后,會(huì)調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation
{
cllocation = userLocation.location;
_clloction = cllocation;
_userLatitude = cllocation.coordinate.latitude;
_userLongitude = cllocation.coordinate.longitude;
[self stopLocation];(如果需要實(shí)時(shí)定位不用停止定位服務(wù))
}
/**
*在停止定位后,會(huì)調(diào)用此函數(shù)
*/
- (void)didStopLocatingUser
{
;
}
/**
*定位失敗后,會(huì)調(diào)用此函數(shù)
*@param error 錯(cuò)誤號(hào)
*/
- (void)didFailToLocateUserWithError:(NSError *)error
{
[self stopLocation];
}
以上代碼就是本文ios百度地圖的使用(普通定位、反地理編碼),希望對(duì)大家今后的工作和學(xué)習(xí)有所幫助。
相關(guān)文章
IOS 靜態(tài)庫(kù)和Framework區(qū)別
這篇文章主要介紹了IOS 靜態(tài)庫(kù)和Framework區(qū)別的相關(guān)資料,這里對(duì)動(dòng)態(tài)庫(kù)與靜態(tài)庫(kù)做比較,選擇什么時(shí)候使用庫(kù)文件,需要的朋友可以參考下2016-12-12
iOS WKWebview 白屏檢測(cè)實(shí)現(xiàn)的示例
這篇文章主要介紹了iOS WKWebview 白屏檢測(cè)實(shí)現(xiàn)的示例,幫助大家更好的進(jìn)行ios開(kāi)發(fā),感興趣的朋友可以了解下2020-10-10
iOS自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的幾種情況
這篇文章主要給大家介紹了關(guān)于iOS自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的幾種情況,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
iOS設(shè)計(jì)模式——Category簡(jiǎn)單介紹
這篇文章主要介紹了iOS設(shè)計(jì)模式——Category簡(jiǎn)單介紹,有興趣學(xué)習(xí)的同學(xué)可以了解一下。2016-11-11
實(shí)例講解iOS中的UIPageViewController翻頁(yè)視圖控制器
UIPageViewController更像是一個(gè)視圖容器,將每頁(yè)不同的ViewController整合,這里我們將以實(shí)例講解iOS中的UIPageViewController翻頁(yè)視圖控制器:2016-06-06
iOS開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

