先來看 iOS 的普通定位(不含地圖)與反地理編碼(取得具體位置)。使用到的頭文件為 <CoreLocation/CoreLocation.h>,需引入 CoreLocation 框架,並實作 CLLocationManagerDelegate 代理。
使用前需在 info.plist 中加上定位權限說明:NSLocationWhenInUseUsageDescription(使用應用期間)或 NSLocationAlwaysUsageDescription(始終)。接著建立 LocationManager 物件管理定位服務:
_manager = [[CLLocationManager alloc] init];
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];
NSLog(@"%@", [test objectForKey:@"Name"]); // 取得完整地址名稱
}
}];
}
百度地圖基本功能
以下以程式碼建立地圖類別,設定地圖屬性、傳入定位座標並調整定位圓點:
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_mapView];
- (void)setMapViewProperty {
_mapView.mapType = BMKUserTrackingModeFollowWithHeading;
_mapView.showsUserLocation = YES; // 是否顯示定位圖層
_mapView.zoomLevel = 16; // 地圖顯示比例
_mapView.rotateEnabled = NO; // 是否可旋轉
[self passLocationValue];
}
// 傳入定位座標,避免地圖總是先顯示天安門
- (void)passLocationValue {
BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake([UserLocationManager sharedInstance].clloction.coordinate, BMKCoordinateSpanMake(0.02f, 0.02f));
BMKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
}
// 設定定位圓點屬性
- (void)setUserImage {
BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
param.locationViewOffsetX = 0;
param.locationViewOffsetY = 0;
param.isAccuracyCircleShow = NO; // 是否顯示精度圈
param.isRotateAngleValid = NO;
[_mapView updateLocationViewWithParam:param];
}
若需在地圖上响應事件,可實作 BMKMapViewDelegate,例如:
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { /* 區域即將改變 */ }
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { /* 區域改變完成 */ }
- (void)mapStatusDidChanged:(BMKMapView *)mapView { /* 地圖狀態改變完成 */ }
地圖定位封裝
實務上建議將定位封裝為獨立的 manager 類別,把定位功能與地圖 mapView 分離,集中管理地理位置的變化:
@interface UserLocationManager : NSObject <BMKMapViewDelegate, BMKLocationServiceDelegate>
@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;
}
- (void)initBMKUserLocation {
_locService = [[BMKLocationService alloc] init];
_locService.delegate = self;
[self startLocation];
}
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation {
_clloction = userLocation.location;
_userLatitude = _clloction.coordinate.latitude;
_userLongitude = _clloction.coordinate.longitude;
[self stopLocation]; // 若需即時定位可不停止
}
@end
透過上述封裝,即可在 iOS 中靈活整合百度地圖的普通定位與反地理編碼功能,並將定位邏輯與地圖視圖解耦,便於維護與擴充。