在iOS開發中會遇到需要使用地圖顯示需要展示的地理位置的情況,這裏使用的是百度地圖SDK,高德地圖也大同小異。
首先,我們去百度地圖的官網上了解一下集成的介紹,下載SDK,沒有註冊開發者的註冊一下,就可以提取APPKey密鑰了。
按照開發文檔的操作導入相關SDK的庫類和依賴庫,引入mapapi.bundle資源文件,從BaiduMapAPI_Map.framework||Resources文件中選擇mapapi.bundle文件導入到工程中,因爲導入庫類時並沒有把這個圖片資源移入進去,反正按文檔來就對了。
把工程中的一個類後綴改爲.mm混合編譯,這裏我們把APPDelegate.m文件改了。
按文檔配置好開發環境後,就開始寫代碼了。
在APPDelegate文件中,
引入頭文件
#import <BaiduMapAPI_Base/BMKMapManager.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch..
//======== 百度地圖 ======================
BMKMapManager * mapManager = [[BMKMapManager alloc] init];
BOOL result = [mapManager start:@"你申請的APPKey" generalDelegate:nil];
if (!result) {
NSLog(@"百度引擎啓動失敗!");
}
return YES;
}
這樣百度地圖引擎就開啓了。
然後在你需要打開百度地圖的viewController寫相關代碼,
這裏我建立了一個MapShowViewController的類,在.m文件中寫相關需要實現的功能代碼。我簡單的實現了把幾個座標用大頭針標註在地圖上的功能,並且定位了手機當前的位置。
效果如下,
#import "MapShowViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
@interface MapShowViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
BMKMapView * mapView;
BMKLocationService * locService;
NSMutableArray * venuesLocationArray;
}
@end
@implementation MapShowViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"附近場館";
self.automaticallyAdjustsScrollViewInsets = YES;
[self initInterFace];
venuesLocationArray = [[NSMutableArray alloc] init];
[venuesLocationArray setArray:@[@{@"lat":@"22.536288",@"lnt":@"114.039077",@"name":@"羅湖高爾夫"},
@{@"lat":@"22.575744477169",@"lnt":@"114.04361926289",@"name":@"深圳高爾夫"}]];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[mapView viewWillAppear];
locService.delegate = self;
mapView.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
for (int i = 0; i < venuesLocationArray.count; i++) {
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = [venuesLocationArray[i][@"lat"] doubleValue];
coor.longitude = [venuesLocationArray[i][@"lnt"] doubleValue];
annotation.coordinate = coor;
annotation.title = venuesLocationArray[i][@"name"];
[mapView addAnnotation:annotation];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[mapView viewWillDisappear];
locService.delegate = nil;
mapView.delegate = nil;
}
- (void)initInterFace{
//初始化定位 BMKLocationService
locService = [[BMKLocationService alloc]init];
locService.distanceFilter = 200;//設定定位的最小更新距離,這裏設置 200m 定位一次,頻繁定位會增加耗電量
locService.desiredAccuracy = kCLLocationAccuracyHundredMeters;//設定定位精度
//啓動LocationService
[locService startUserLocationService];
//初始化地圖
mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];
mapView.showMapScaleBar = YES; // 設定是否顯式比例尺
mapView.userTrackingMode = BMKUserTrackingModeNone;//設置定位的狀態
mapView.zoomLevel = 14;
mapView.showsUserLocation = YES;
self.view = mapView;
}
#pragma mark - MapView協議方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
newAnnotationView.animatesDrop = YES;// 設置該標註點動畫顯示
// newAnnotationView.image = [UIImage imageNamed:@"locate.png"];//設置大頭針圖片
return newAnnotationView;
}
return nil;
}
#pragma mark - location協議方法
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
[mapView updateLocationData:userLocation];// 動態更新我的位置數據
[mapView setCenterCoordinate:userLocation.location.coordinate];// 當前地圖的中心點
}
@end
功能暫時只做了這些,如有不足請多見諒。