您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 百度地圖的使用(普通定位、反地理編碼)

編輯:iOS 相關教學

本文透過程式碼講解 iOS 定位中的普通定位(沒有地圖)與反地理編碼(取得具體位置)。

一、使用 CoreLocation 做普通定位與反地理編碼

#import <CoreLocation/CoreLocation.h> // 需引入 CoreLocation

// 使用代理 CLLocationManagerDelegate

// 1. 使用定位服務
// 在 info.plist 中添加以下兩條(或其中一條):
// NSLocationWhenInUseUsageDescription —— 在使用應用期間
// NSLocationAlwaysUsageDescription    —— 始終

// 2. 建立 LocationManager 物件管理定位服務
_manager = [[CLLocationManager alloc] init];

// 判斷手機是否開啟定位、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"]);
        }
    }];
}

二、百度地圖的基本功能

新建一個地圖類(可用 xib 拖,也可用程式碼實作):

_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[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 的協議方法:

// 地圖區域即將改變時呼叫
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { }

// 地圖區域改變完成後呼叫
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { }

// 地圖狀態改變完成後呼叫
- (void)mapStatusDidChanged:(BMKMapView *)mapView { }

三、地圖定位

這裡把定位封裝成一個獨立的 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

@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;
}

- (void)initBMKUserLocation {
    _locService = [[BMKLocationService alloc] init];
    _locService.delegate = self;
    [self startLocation];
}

- (void)startLocation { [_locService startUserLocationService]; }
- (void)stopLocation  { [_locService stopUserLocationService]; }

// 用戶位置更新後呼叫
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation {
    cllocation = userLocation.location;
    _clloction = cllocation;
    _userLatitude = cllocation.coordinate.latitude;
    _userLongitude = cllocation.coordinate.longitude;
    [self stopLocation]; // 若需實時定位可不停止
}

- (void)didStopLocatingUser { }

// 定位失敗後呼叫
- (void)didFailToLocateUserWithError:(NSError *)error {
    [self stopLocation];
}
@end

以上就是 iOS 百度地圖使用(普通定位、反地理編碼)的介紹,希望對大家日後的工作與學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved