百度SDK下載地址:
*http://lbsyun.baidu.com/apiconsole/key
使用步驟: (注意: 是在純代碼的基礎(chǔ)上進(jìn)行的, 如果使用StoryBoard和Xib另當(dāng)別論)
-
創(chuàng)建應(yīng)用:需要注意兩點(diǎn)(指定項(xiàng)目名 + 找到項(xiàng)目中g(shù)eneral--> Bundle Identifier--> 復(fù)制到安全碼處)
創(chuàng)建應(yīng)用.png -
靜態(tài)庫(kù)中采用ObjectC++實(shí)現(xiàn), 需要工程中有一個(gè)文件的后綴為.mm的源文件(任意一個(gè).m文件改為.mm 即可)
.mm文件.png -
配置info.plist文件下面兩選項(xiàng)
![Uploading AAB531A6-A091-4E63-AD76-4FEC229FD1D9_873212.png . . .]
咣咣 !配置完成, 下面實(shí)現(xiàn)代碼部分
AppDelegate.m文件
#import "AppDelegate.h"
#第一步: 導(dǎo)入頭文件
#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface AppDelegate ()
#第二步: 創(chuàng)建管理者
@property (strong, nonatomic) BMKMapManager * mapManager;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark-- 百度地圖
// 要使用百度地圖词顾,請(qǐng)先啟動(dòng)BaiduMapManager
_mapManager = [[BMKMapManager alloc]init];
// 如果要關(guān)注網(wǎng)絡(luò)及授權(quán)驗(yàn)證事件,請(qǐng)?jiān)O(shè)定 generalDelegate參數(shù)
BOOL ret = [_mapManager start:@"yFiVBGc1DIznyAxKeUIXWCwrdDcT7YXl" generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
return YES;
}
#import "MapViewController.h"
@interface MapViewController ()<BMKMapViewDelegate,BMKGeoCodeSearchDelegate>
@property (nonatomic,strong)BMKGeoCodeSearch * geoCodeSearch;//編碼搜索對(duì)象
@end
@implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.mapView.zoomLevel = 17;//比例
[self.view addSubview: self.mapView];
//檢索工具
self.geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
_geoCodeSearch.delegate = self;
//(檢索信息)類(lèi)對(duì)象
BMKGeoCodeSearchOption * option = [[BMKGeoCodeSearchOption alloc] init];
option.city = @"北京市";
option.address = @"朝陽(yáng)區(qū)亮馬家園35號(hào)樓";
BOOL flag = [_geoCodeSearch geoCode:option];
if (flag) {
NSLog(@"檢索成功!");
}else
{
NSLog(@"檢索失數段堋遣耍!");
}
}
#pragma mark---繪制大頭針
//根據(jù)annotation繪制大頭針
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
NSString * annotationViewID = @"annotationID";
//根據(jù)指定標(biāo)識(shí)查找一個(gè)可被復(fù)用的annotation
BMKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
if (!annotationView) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewID];
((BMKPinAnnotationView *)annotationView).pinColor = BMKPinAnnotationColorPurple;
((BMKPinAnnotationView *)annotationView).animatesDrop = YES;
}
//
// annotationView.centerOffset = CGPointMake(0, 10);
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
return annotationView;
}
#pragma mark -- 協(xié)議方法
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
NSLog(@"%@",result);
//將上一次大頭針數(shù)據(jù)清空
NSArray * array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
BMKPointAnnotation * pointAnnotation = [[BMKPointAnnotation alloc] init];
//Annotaion在MVC中相當(dāng)于M伊磺,存儲(chǔ)了大頭針的相關(guān)信息
//設(shè)置經(jīng)緯度
pointAnnotation.coordinate = result.location;
pointAnnotation.title = result.address;
[_mapView addAnnotation:pointAnnotation];
//設(shè)置當(dāng)前地圖的中心點(diǎn)
_mapView.centerCoordinate = result.location;
}
- (void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時(shí)候需要置nil裹驰,否則影響內(nèi)存的釋放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時(shí)井濒,置nil
}
@end