国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios中百度地圖的使用詳解

2020-02-19 15:54:53
字體:
來源:轉載
供稿:網友

相信百度地圖大家都聽說過,也是很熟悉的,本篇文章介紹的是ios中百度地圖的使用詳解,下面就跟著武林技術頻道小編的步伐一起來了解一下吧!

iOS定位 - 普通定位(沒有地圖) - 反地理編碼(得到具體位置),下面通過代碼給大家詳解,代碼如下:

#import  使用到的頭文件 要引入CoreLocation這個包    使用的代理名稱//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 #import "BMapKit.h"@interface UserLocationManager : NSObject { 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中百度地圖的使用詳解的全部內容,希望對大家今后的工作和學習有所幫助,想要了解更多的內容介紹,請繼續關注武林技術頻道吧!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲阜市| 阿合奇县| 绍兴县| 勐海县| 南雄市| 福清市| 丹棱县| 宁河县| 渑池县| 洛扎县| 临潭县| 淳化县| 防城港市| 建昌县| 宁武县| 金门县| 嫩江县| 萍乡市| 社会| 祁阳县| 轮台县| 册亨县| 巴彦县| 景德镇市| 丹巴县| 临夏县| 恭城| 囊谦县| 静宁县| 西昌市| 新干县| 平泉县| 华池县| 平定县| 玉龙| 巴彦淖尔市| 铁岭市| 枣强县| 库尔勒市| 泽州县| 清涧县|