百度地圖的集成(轉(zhuǎn)載)

如何快速集成百度地圖:

注冊(cè)百度開(kāi)發(fā)者帳號(hào)=》創(chuàng)建應(yīng)用=》下載SDK=》集成開(kāi)發(fā)=》測(cè)試應(yīng)用=》發(fā)布應(yīng)用

1、注冊(cè)百度開(kāi)發(fā)者賬號(hào)

百度賬號(hào)注冊(cè)地址:https://passport.baidu.com/v2/?reg?Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F,如果你已經(jīng)有百度賬號(hào)可以跳過(guò)這步。

登錄后,進(jìn)入開(kāi)發(fā)者中心,注冊(cè)成為百度開(kāi)發(fā)者,http://developer.baidu.com/user/info?u=http://lbsyun.baidu.com/apiconsole/key?from=developer,填寫(xiě)好個(gè)人信息,提交。

2、創(chuàng)建新應(yīng)用

在使用百度地圖SDK之前需要先獲取百度地圖移動(dòng)版開(kāi)發(fā)密鑰。

百度地圖iOS SDK開(kāi)發(fā)密鑰的申請(qǐng)地址為:http://lbsyun.baidu.com/apiconsole/key。

點(diǎn)擊以上網(wǎng)址進(jìn)入API控制臺(tái),選擇創(chuàng)建應(yīng)用,填寫(xiě)應(yīng)用信息:

確認(rèn)后創(chuàng)建完成,可以在我的應(yīng)用終看到應(yīng)用的ak:

3、下載IOS SDK

百度地圖iOS SDK的下載地址:http://developer.baidu.com/map/sdkiosdev-download.htm

進(jìn)入后可以下載全部功能,也可以根據(jù)自己需要選擇模塊選擇下載:

4、集成開(kāi)發(fā)

1)新建一個(gè)工程

2)添加百度SDK和靜態(tài)庫(kù)

解壓下載后的iOS SDK壓縮包將壓縮包內(nèi)的inc文件夾和mapapi.bundle文件拷貝到工程目錄下。

接下來(lái)根據(jù)模擬器和真機(jī)兩種環(huán)境選擇使用靜態(tài)庫(kù)文件:

如果用的真機(jī),就導(dǎo)入iphoneos文件夾內(nèi)的libbaidumapapi.a文件到工程,如果用的模擬器,就導(dǎo)入iphonesimulator文件夾內(nèi)的libbaidumapapi.a文件到工程。

引入如下圖所示的framework:

3)導(dǎo)入頭文件,初始化并啟動(dòng)百度地圖

首先在工程的.pch文件中導(dǎo)入頭文件,并宏定義APPKEY:

1.#import"BMapKit.h"

2.#define?APPKEY?@"9D6E6FFvXskzZge2GuwXWsRi"

在Appdelegate.h中添加代碼如下,必須property? BMKMapManager,不然無(wú)法啟動(dòng)地圖服務(wù):

_mapManager?=?[[BMKMapManageralloc]?init?];

BOOLret?=?[_mapManagerstart:APPKEYgeneralDelegate:nil];

if(!ret)?{

NSLog(@"啟動(dòng)失敗");

}

4)創(chuàng)建BMKMapView,顯示百度地圖視圖

#import<UIKit/UIKit.h>

@interfaceBaseMapViewController?:?UIViewController

@property?(nonatomic,?strong)?BMKMapView?*mapView;

@end

在.m文件

-?(void)viewDidLoad

{

?//這里的frame請(qǐng)根據(jù)手機(jī)分辨率動(dòng)態(tài)設(shè)置???? self.mapView.frame?=?CGRectMake(0,?0,?320,?460-44);

self.mapView.delegate?=?self;

[self.viewaddSubview:self.mapView];

}

-?(void)viewWillAppear:(BOOL)animated

{

[_mapViewviewWillAppear];

_mapView.delegate?=?self;//?此處記得不用的時(shí)候需要置nil,否則影響內(nèi)存的釋放

}

-?(void)viewWillDisappear:(BOOL)animated

{

[_mapViewviewWillDisappear];

_mapView.delegate?=?nil;//?不用時(shí),置nil

}

5)更改地圖顯示方式

switch(index)?{

case0:{

//設(shè)置地圖顯示類(lèi)型,有四種標(biāo)準(zhǔn)地圖、實(shí)時(shí)路況、衛(wèi)星地圖、同時(shí)打開(kāi)實(shí)時(shí)路況和衛(wèi)星地圖

self.mapView.mapType?=?BMKMapTypeStandard;

break;

}

case1:{

self.mapView.mapType?=?BMKMapTypeTrafficOn;

break;

}

case2:{

self.mapView.mapType?=?BMKMapTypeSatellite;

break;

}

default:{

self.mapView.mapType?=?BMKMapTypeTrafficAndSatellite;

break;

}

}

6)添加地圖標(biāo)注

首先設(shè)置標(biāo)注的基本屬性

在.h文件描述一下

@property?(nonatomic,?strong)?BMKPointAnnotation?*annotation;

在.m文件

self.annotation?=?[BMKPointAnnotationnew];

CLLocationCoordinate2Dcoor;

coor.latitude?=?39.911447;

coor.longitude?=?116.406026;

//設(shè)置標(biāo)注的坐標(biāo)

self.annotation.coordinate?=?coor;

//標(biāo)題

self.annotation.title?=?@"北京";

//副標(biāo)題

self.annotation.subtitle?=?@"這個(gè)annotation可以拖拽";

[self.mapViewaddAnnotation:self.annotation];

詳細(xì)設(shè)置標(biāo)注,以下方法是SDK自帶的,方法內(nèi)容需要自己寫(xiě)

-?(BMKAnnotationView?*)mapView:(BMKMapView?*)mapViewviewForAnnotation:(id)annotation{

if([annotation?isKindOfClass:[BMKPointAnnotationclass]])?{

BMKPinAnnotationView?*newAnnotationView?=?[[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];

newAnnotationView.pinColor?=?BMKPinAnnotationColorGreen;

//動(dòng)畫(huà)效果

newAnnotationView.animatesDrop?=?YES;

//可以拖拽

newAnnotationView.draggable?=?YES;

//3D效果

newAnnotationView.enabled3D?=?YES;

returnnewAnnotationView;

}

returnnil;

}

在不用標(biāo)注的時(shí)候,把它從視圖上移除

-?(void)viewDidDisappear:(BOOL)animated

{

[superviewDidDisappear:animated];

if(self.annotation!=nil)?{

[self.mapViewremoveAnnotation:self.annotation];

}

}

7)POI興趣點(diǎn)檢索

百度地圖SDK提供了三種POI搜索(周邊、區(qū)域、城市內(nèi)搜索),搜索方式都類(lèi)似,下面以周邊搜索做個(gè)示例。

在.h文件:

#import"BaseMapViewController.h"

@interfacePoiViewController?:?BaseMapViewController

@property?(nonatomic,?strong)?BMKPoiSearch?*searcher;

@end

在.m文件

-?(void)viewDidLoad

{

[superviewDidLoad];

self.searcher?=?[BMKPoiSearchnew];

self.searcher.delegate?=?self;

//搜索周邊

BMKNearbySearchOption?*option?=?[BMKNearbySearchOptionnew];

//搜索周邊時(shí)的中心點(diǎn)坐標(biāo)

option.location?=?CLLocationCoordinate2DMake(39.911447,?116.406026);

//搜索的關(guān)鍵字

option.keyword?=?@"肯德基";

BOOLflag?=?[self.searcherpoiSearchNearBy:option];

if(!flag)?{

NSLog(@"周邊檢索失敗");

}

}

搜索到結(jié)果之后的代理方法

-?(void)onGetPoiResult:(BMKPoiSearch?*)searcher?result:(BMKPoiResult?*)poiResulterrorCode:(BMKSearchErrorCode)errorCode{

//在添加標(biāo)注前把以前的標(biāo)注都移除掉

NSMutableArray?*poiAnnotations?=?[NSMutableArrayarrayWithCapacity:poiResult.poiInfoList.count];

[self.mapViewremoveAnnotations:poiAnnotations];

if(errorCode?==?BMK_SEARCH_NO_ERROR)?{

NSLog(@"找到結(jié)果");

for(inti?=?0;?i

BMKPoiInfo?*info?=?[poiResult.poiInfoListobjectAtIndex:i];

BMKPointAnnotation?*pointAnnotation?=?[BMKPointAnnotationnew];

pointAnnotation.coordinate?=?info.pt;

pointAnnotation.title?=?info.name;

pointAnnotation.subtitle?=?[NSStringstringWithFormat:@"%@%@",info.city,info.address];

[poiAnnotationsaddObject:pointAnnotation];

//設(shè)置第一個(gè)搜索到的興趣點(diǎn)為中心點(diǎn)

if(i==0)?{

self.mapView.centerCoordinate?=?info.pt;

}

}

[self.mapViewaddAnnotations:poiAnnotations];

}elseif?(errorCode?==?BMK_SEARCH_AMBIGUOUS_KEYWORD){

NSLog(@"起始點(diǎn)有歧義,有相同名字的別的城市:%@",poiResult.cityList);

}else{

NSLog(@"未找到結(jié)果");

}

}

-?(void)viewWillDisappear:(BOOL)animated

{

[superviewWillDisappear:animated];

NSArray?*array?=?[NSArrayarrayWithArray:self.mapView.annotations];

[self.mapViewremoveAnnotations:array];

self.searcher.delegate?=?nil;//不用的時(shí)候代理取消

}

8)路徑規(guī)劃

路徑規(guī)劃有三種類(lèi)型(公交、駕車(chē)、步行),使用方式都類(lèi)似,下面以駕車(chē)做個(gè)示例。

在.h文件

#import"BaseMapViewController.h"

@interfaceWayPointViewController?:?BaseMapViewController

@property?(nonatomic,?strong)?BMKRouteSearch?*routeSearch;

@end

在.m文件

-?(void)viewDidLoad

{

[superviewDidLoad];

self.routeSearch?=?[BMKRouteSearchnew];

self.routeSearch.delegate?=?self;

//線(xiàn)路起點(diǎn)信息

BMKPlanNode?*start?=?[BMKPlanNodenew];

start.name?=?@"天安門(mén)";

start.cityName?=?@"北京市";

//終點(diǎn)信息

BMKPlanNode?*end?=?[BMKPlanNodenew];

end.name?=?@"百度大廈";

end.cityName?=?@"北京市";

//途經(jīng)點(diǎn)信息

BMKPlanNode?*wayPoint1?=?[BMKPlanNodenew];

wayPoint1.cityName?=?@"北京市";

wayPoint1.name?=?@"東直門(mén)";

NSMutableArray?*array?=?[NSMutableArraynew];

[arrayaddObject:wayPoint1];

//駕車(chē)查詢(xún)

BMKDrivingRoutePlanOption?*drivePlan?=?[[BMKDrivingRoutePlanOptionalloc]init];

drivePlan.from?=?start;

drivePlan.to?=?end;

drivePlan.wayPointsArray?=?array;

BOOLflag?=?[self.routeSearchdrivingSearch:drivePlan];

if(!flag)?{

NSLog(@"公交檢索發(fā)送失敗");

}

}

//根據(jù)搜索返回的結(jié)果繪制折線(xiàn)

-?(void)onGetDrivingRouteResult:(BMKRouteSearch?*)searcher?result:(BMKDrivingRouteResult?*)result?errorCode:(BMKSearchErrorCode)error

{

//移除地圖的標(biāo)注和覆蓋物

NSArray?*array?=?[NSArrayarrayWithArray:self.mapView.annotations];

[self.mapViewremoveAnnotations:array];

array?=?[NSArrayarrayWithArray:self.mapView.overlays];

[self.mapViewremoveAnnotations:array];

if(error?==?BMK_SEARCH_NO_ERROR)?{

BMKDrivingRouteLine?*plan?=?[result.routesobjectAtIndex:0];

intsize?=?plan.steps.count;

//軌跡點(diǎn)

CLLocationCoordinate2DpolylineCoords[size];

for(inti=0?;i

BMKDrivingStep?*drivingStep?=?[plan.stepsobjectAtIndex:i];

//添加annotation節(jié)點(diǎn)

BMKPointAnnotation*?item?=?[BMKPointAnnotationnew];

item.coordinate?=?drivingStep.entrace.location;

polylineCoords[i]=?drivingStep.entrace.location;

item.title?=?drivingStep.entraceInstruction;

[self.mapViewaddAnnotation:item];

//?添加起點(diǎn)標(biāo)注

if(i==0)?{

item.title?=?@"起點(diǎn)";

[self.mapViewselectAnnotation:itemanimated:YES];

}

//?添加起點(diǎn)標(biāo)注

if(i==size-1){

item.title?=?@"終點(diǎn)";

}

}

//繪制折線(xiàn)

BMKPolyline*?polyLine?=?[BMKPolylinepolylineWithCoordinates:polylineCoordscount:size];

[self.mapViewaddOverlay:polyLine];

}

}

//定義折線(xiàn)樣式

-?(BMKOverlayView*)mapView:(BMKMapView?*)map?viewForOverlay:(id)overlay

{

if([overlay?isKindOfClass:[BMKPolylineclass]])?{

BMKPolylineView*?polylineView?=?[[BMKPolylineViewalloc]?initWithOverlay:overlay];

polylineView.fillColor?=?[UIColorblackColor];

polylineView.strokeColor?=?[UIColorblueColor];

polylineView.lineWidth?=?6.0f;

returnpolylineView;

}

returnnil;

}

-?(void)viewDidDisappear:(BOOL)animated

{

[superviewDidDisappear:animated];

NSArray?*array?=?[NSArrayarrayWithArray:self.mapView.annotations];

[self.mapViewremoveAnnotations:array];

array?=?[NSArrayarrayWithArray:self.mapView.overlays];

[self.mapViewremoveAnnotations:array];

self.routeSearch.delegate?=?nil;//不用的時(shí)候代理取消

}

9)地圖定位

在.h文件

#import"BaseMapViewController.h"

@interfaceUserLocationViewController?:?BaseMapViewController

@property?(nonatomic,?strong)BMKLocationService?*localService;

@end

在.m文件

self.localService?=?[BMKLocationServicenew];

self.localService.delegate?=?self;

[self.localServicestartUserLocationService];

//改變地圖定位樣式的時(shí)候必須先關(guān)閉圖層,改變樣式后再打開(kāi)

//關(guān)閉顯示的定位圖層

self.mapView?.showsUserLocation?=?NO;

switch(index)?{

case0:{

//三種定位模式:普通、跟隨、羅盤(pán)

self.mapView.userTrackingMode?=?BMKUserTrackingModeNone;

break;

}

case1:{

self.mapView.userTrackingMode?=?BMKUserTrackingModeFollowWithHeading;

break;

}

default:{

self.mapView.userTrackingMode?=?BMKUserTrackingModeFollow;

break;

}

}

//打開(kāi)顯示的定位圖層

self.mapView?.showsUserLocation?=?YES;

10)短串分享

短串分享是在用戶(hù)搜索查詢(xún)后得到的每一個(gè)地理位置結(jié)果會(huì)對(duì)應(yīng)一個(gè)短連接,用戶(hù)可以通過(guò)短信等方式分享給別人。當(dāng)用戶(hù)受到分享的短串后,點(diǎn)擊短串可以打開(kāi)百度地圖客戶(hù)端或手機(jī)瀏覽器進(jìn)行查看。

定義幾個(gè)屬性用來(lái)存放搜素的數(shù)據(jù)

//名稱(chēng)

NSString*?geoName;

//地址

NSString*?addr;

//坐標(biāo)

CLLocationCoordinate2Dpt;

//短串

NSString*?shortUrl;

//分享字符串

NSString*?showmeg;

首先初始化對(duì)象,并發(fā)起一個(gè)搜索

self.shareSearch?=?[BMKShareURLSearchnew];

self.shareSearch.delegate?=?self;

self.poiSearch?=?[BMKPoiSearchnew];

self.poiSearch.delegate?=?self;

BMKNearbySearchOption?*option?=?[BMKNearbySearchOptionnew];

option.pageIndex?=?0;

option.pageCapacity?=?10;

option.location?=?CLLocationCoordinate2DMake(39.911447,?116.406026);

option.keyword?=?@"肯德基";

BOOLflag?=?[self.poiSearchpoiSearchNearBy:option];

if(!flag)?{

NSLog(@"周邊檢索失敗");

}

搜索成功后,處理數(shù)據(jù),獲取要分享的url,發(fā)起分享

-?(void)onGetPoiResult:(BMKPoiSearch?*)searcher?result:(BMKPoiResult?*)poiResulterrorCode:(BMKSearchErrorCode)errorCode{

if(errorCode?==?BMK_SEARCH_NO_ERROR)?{

for(inti?=?0;?i

BMKPoiInfo?*info?=?[poiResult.poiInfoListobjectAtIndex:i];

BMKPointAnnotation?*pointAnnotation?=?[BMKPointAnnotationnew];

pointAnnotation.coordinate?=?info.pt;

pointAnnotation.title?=?info.name;

pointAnnotation.subtitle?=?[NSStringstringWithFormat:@"%@%@",info.city,info.address];

[poiAnnotationsaddObject:pointAnnotation];

if(i==0)?{

self.mapView.centerCoordinate?=?info.pt;

geoName?=?info.name;

addr?=?info.address;

pt?=?info.pt;

BMKPoiDetailShareURLOption?*option?=?[BMKPoiDetailShareURLOptionnew];

option.uid?=?info.uid;

BOOLflag?=?[self.shareSearchrequestPoiDetailShareURL:option];

if(!flag)?{

NSLog(@"詳情url檢索發(fā)送失敗");

}}}}}

處理分享的方法

-(void)onGetPoiDetailShareURLResult:(BMKShareURLSearch?*)searcher?result:(BMKShareURLResult?*)result?errorCode:(BMKSearchErrorCode)error

{

shortUrl?=?result.url;

if(error?==?BMK_SEARCH_NO_ERROR)

{

showmeg?=?[NSStringstringWithFormat:@"這里是:%@\r\n%@\r\n%@",geoName,addr,shortUrl];

UIAlertView?*myAlertView?=?[[UIAlertViewalloc]?initWithTitle:@"短串分享"message:showmegdelegate:selfcancelButtonTitle:nilotherButtonTitles:@"分享",@"取消",nil];

myAlertView.tag?=?1000;

[myAlertViewshow];

}}

判斷如果設(shè)備可以發(fā)送短信,則發(fā)送分享

-?(void)alertView:(UIAlertView?*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex

{

if(alertView.tag?==1000?)

{

if(buttonIndex?==?0)

{

Class?messageClass?=?(NSClassFromString(@"MFMessageComposeViewController"));

if(messageClass?!=?nil)?{

if([messageClasscanSendText])?{

MFMessageComposeViewController?*picker?=?[[MFMessageComposeViewControlleralloc]?init];

picker.messageComposeDelegate?=?self;

picker.body?=?[NSStringstringWithFormat:@"%@",showmeg];

[selfpresentModalViewController:pickeranimated:YES];

}else{

UIAlertView?*myAlertView?=?[[UIAlertViewalloc]?initWithTitle:@"當(dāng)前設(shè)備無(wú)法發(fā)送短信"message:nildelegate:selfcancelButtonTitle:nilotherButtonTitles:@"確定",nil];

[myAlertViewshow];

}}}}}

11)調(diào)啟導(dǎo)航

調(diào)啟導(dǎo)航分為兩種:百度地圖客戶(hù)端和Web導(dǎo)航。

首先要配置info.plist,自定義一個(gè)URL Scheme和APP綁定,選擇工程Targets》Info》URL Types,點(diǎn)擊+號(hào)添加一個(gè)url type。

如圖所示:

下面開(kāi)始編寫(xiě)代碼,

#defineMAPScheme?@"

baidumapsdk://mapsdk.baidu.com

"

@property?(nonatomic,?strong)BMKNaviPara?*para;

//判讀是否能打開(kāi)百度地圖客戶(hù)端

-?(BOOL)canOpenBaiDuMap

{

return[[UIApplicationsharedApplication]?canOpenURL:[NSURLURLWithString:MAPScheme]];

}

-?(void)viewDidLoad

{

[superviewDidLoad];

self.para?=?[BMKNaviParanew];

BOOLflag?=?[selfcanOpenBaiDuMap];

if(flag)?{

//使用百度地圖客戶(hù)端導(dǎo)航

self.para.naviType?=?BMK_NAVI_TYPE_NATIVE;

//初始化終點(diǎn)節(jié)點(diǎn)

BMKPlanNode*?end?=?[BMKPlanNodenew];

//指定終點(diǎn)經(jīng)緯度

CLLocationCoordinate2D?coor2;

coor2.latitude?=?116.3956;

coor2.longitude?=?39.90868;

end.pt?=?coor2;

//指定終點(diǎn)名稱(chēng)

end.name?=?@"北京市天安門(mén)";

//指定終點(diǎn)

self.para.endPoint?=?end;

//指定返回自定義scheme,具體定義方法請(qǐng)參考常見(jiàn)問(wèn)題

self.para.appScheme?=?MAPScheme;

//調(diào)啟百度地圖客戶(hù)端導(dǎo)航

[BMKNavigationopenBaiduMapNavigation:self.para];

}else{

//使用WEB導(dǎo)航

self.para.naviType?=?BMK_NAVI_TYPE_WEB;

//初始化起點(diǎn)節(jié)點(diǎn)

BMKPlanNode*?start?=?[BMKPlanNodenew];

//指定起點(diǎn)經(jīng)緯度

CLLocationCoordinate2D?coor1;

coor1.latitude?=?116.204;

coor1.longitude?=?39.90868;

start.pt?=?coor1;

//指定起點(diǎn)名稱(chēng)

start.name?=?@"西直門(mén)";

//指定起點(diǎn)

self.para.startPoint?=?start;

//初始化終點(diǎn)節(jié)點(diǎn)

BMKPlanNode*?end?=?[BMKPlanNodenew];

CLLocationCoordinate2D?coor2;

coor2.latitude?=?116.3956;

coor2.longitude?=?39.90868;

end.pt?=?coor2;

self.para.endPoint?=?end;

//指定終點(diǎn)名稱(chēng)

end.name?=?@"北京市天安門(mén)";

//指定調(diào)啟導(dǎo)航的app名稱(chēng)

self.para.appName?=?@"BaiDuMap";

//調(diào)啟web導(dǎo)航

[BMKNavigationopenBaiduMapNavigation:self.para];

}

}

主要功能

實(shí)時(shí)路況:

衛(wèi)星地圖:

地圖標(biāo)注:

功能特色

興趣點(diǎn)搜索:

路徑規(guī)劃:

短串分享:

DEMO展示

部分測(cè)試DEMO展示:

#import"ShareUrlViewController.h"

#import

#import

@interfaceShareUrlViewController?()

{

//名稱(chēng)

NSString*?geoName;

//地址

NSString*?addr;

//坐標(biāo)

CLLocationCoordinate2Dpt;

//短串

NSString*?shortUrl;

//分享字符串

NSString*?showmeg;

}

@end

@implementationShareUrlViewController

-?(BMKAnnotationView?*)mapView:(BMKMapView?*)mapViewviewForAnnotation:(id)annotation{

if([annotation?isKindOfClass:[BMKPointAnnotationclass]])?{

BMKPinAnnotationView?*newAnnotationView?=?[[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];

newAnnotationView.pinColor?=?BMKPinAnnotationColorGreen;

//動(dòng)畫(huà)效果

newAnnotationView.animatesDrop?=?YES;

//3D效果

newAnnotationView.enabled3D?=?YES;

//單擊彈出泡泡,默認(rèn)是YES

//newAnnotationView.canShowCallout?=?YES;

returnnewAnnotationView;

}

returnnil;

}

-?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil

{

self?=?[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if(self)?{

//?Custom?initialization

}

returnself;

}

-?(void)viewDidLoad

{

[superviewDidLoad];

self.shareSearch?=?[BMKShareURLSearchnew];

self.shareSearch.delegate?=?self;

self.poiSearch?=?[BMKPoiSearchnew];

self.poiSearch.delegate?=?self;

BMKNearbySearchOption?*option?=?[BMKNearbySearchOptionnew];

option.pageIndex?=?0;

option.pageCapacity?=?10;

option.location?=?CLLocationCoordinate2DMake(39.911447,?116.406026);

option.keyword?=?@"肯德基";

BOOLflag?=?[self.poiSearchpoiSearchNearBy:option];

if(!flag)?{

NSLog(@"周邊檢索失敗");

}

}

-?(void)onGetPoiResult:(BMKPoiSearch?*)searcher?result:(BMKPoiResult?*)poiResulterrorCode:(BMKSearchErrorCode)errorCode{

NSMutableArray?*poiAnnotations?=?[NSMutableArrayarrayWithCapacity:poiResult.poiInfoList.count];

//移除地圖的標(biāo)注和覆蓋物

NSArray?*array?=?[NSArrayarrayWithArray:self.mapView.annotations];

[self.mapViewremoveAnnotations:array];

if(errorCode?==?BMK_SEARCH_NO_ERROR)?{

NSLog(@"找到結(jié)果");

for(inti?=?0;?i

BMKPoiInfo?*info?=?[poiResult.poiInfoListobjectAtIndex:i];

BMKPointAnnotation?*pointAnnotation?=?[BMKPointAnnotationnew];

pointAnnotation.coordinate?=?info.pt;

pointAnnotation.title?=?info.name;

pointAnnotation.subtitle?=?[NSStringstringWithFormat:@"%@%@",info.city,info.address];

[poiAnnotationsaddObject:pointAnnotation];

if(i==0)?{

self.mapView.centerCoordinate?=?info.pt;

geoName?=?info.name;

addr?=?info.address;

pt?=?info.pt;

BMKPoiDetailShareURLOption?*option?=?[BMKPoiDetailShareURLOptionnew];

option.uid?=?info.uid;

BOOLflag?=?[self.shareSearchrequestPoiDetailShareURL:option];

if(!flag)?{

NSLog(@"詳情url檢索發(fā)送失敗");

}

}

}

[self.mapViewaddAnnotations:poiAnnotations];

[self.mapViewselectAnnotation:[poiAnnotationsobjectAtIndex:0]?animated:YES];

}elseif?(errorCode?==?BMK_SEARCH_AMBIGUOUS_KEYWORD){

NSLog(@"起始點(diǎn)有歧義,有相同名字的別的城市:%@",poiResult.cityList);

}else{

NSLog(@"未找到結(jié)果");

}

}

-?(void)onGetPoiDetailShareURLResult:(BMKShareURLSearch?*)searcher?result:(BMKShareURLResult?*)result?errorCode:(BMKSearchErrorCode)error

{

shortUrl?=?result.url;

if(error?==?BMK_SEARCH_NO_ERROR)

{

showmeg?=?[NSStringstringWithFormat:@"這里是:%@\r\n%@\r\n%@",geoName,addr,shortUrl];

UIAlertView?*myAlertView?=?[[UIAlertViewalloc]?initWithTitle:@"短串分享"message:showmegdelegate:selfcancelButtonTitle:nilotherButtonTitles:@"分享",@"取消",nil];

myAlertView.tag?=?1000;

[myAlertViewshow];

}

}

-?(void)alertView:(UIAlertView?*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex

{

if(alertView.tag?==1000?)

{

if(buttonIndex?==?0)

{

Class?messageClass?=?(NSClassFromString(@"MFMessageComposeViewController"));

if(messageClass?!=?nil)?{

if([messageClasscanSendText])?{

MFMessageComposeViewController?*picker?=?[[MFMessageComposeViewControlleralloc]?init];

picker.messageComposeDelegate?=?self;

picker.body?=?[NSStringstringWithFormat:@"%@",showmeg];

[selfpresentModalViewController:pickeranimated:YES];

}else{

UIAlertView?*myAlertView?=?[[UIAlertViewalloc]?initWithTitle:@"當(dāng)前設(shè)備無(wú)法發(fā)送短信"message:nildelegate:selfcancelButtonTitle:nilotherButtonTitles:@"確定",nil];

[myAlertViewshow];

}}}}}

-?(void)messageComposeViewController:(MFMessageComposeViewController?*)controller

didFinishWithResult:(MessageComposeResult)result

{

switch(result)?{

caseMessageComposeResultCancelled://用戶(hù)自己取消,不用提醒

break;

caseMessageComposeResultSent://后續(xù)可能不能夠成功發(fā)送,所以暫時(shí)不提醒

break;

caseMessageComposeResultFailed:?NSLog(@"短信發(fā)送失敗");

break;

default:??NSLog(@"短信沒(méi)有發(fā)送");

break;

}

[selfdismissModalViewControllerAnimated:YES];

}

-(void)viewWillDisappear:(BOOL)animated?{

[superviewWillDisappear:animated];

NSArray?*array?=?[NSArrayarrayWithArray:self.mapView.annotations];

[self.mapViewremoveAnnotations:array];

self.poiSearch.delegate?=?nil;

self.shareSearch.delegate?=?nil;

}

測(cè)試日志

周邊興趣點(diǎn)搜索:

遇到問(wèn)題

1.剛開(kāi)始開(kāi)啟百度服務(wù)的時(shí)候,一直無(wú)法顯示百度地圖圖層,筆者確定了APPKEY正確,keyStatus也返回的是0,但就是無(wú)法顯示。

最后確定出錯(cuò)原因在ARC,在開(kāi)啟ARC的項(xiàng)目中,BMKMapManager一定要在頭文件里面聲明屬性。

2.在使用調(diào)起百度導(dǎo)航的時(shí)候需要配置URL

scheme并使用,在代碼中需要設(shè)置appScheme,筆者按照URL

Schemes+”://”+Identifier的方式拼接字符串,發(fā)現(xiàn)不能調(diào)啟。原因是Identifier拼接的時(shí)候要反向?qū)?,?/p>

如”com.baidu.mapsdk”要改寫(xiě)成”mapsdk.baidu.com”。

上手難易

百度地圖上手難度一般,需要掌握面向?qū)ο蟮拈_(kāi)發(fā)模式,按照百度SDK提供的相關(guān)接口調(diào)用方法。代理實(shí)現(xiàn)返回的功能則需要開(kāi)發(fā)者自己去定義。

開(kāi)發(fā)文檔

關(guān)于百度地圖iOS SDK的開(kāi)發(fā)文檔,請(qǐng)參考鏈接:

http://developer.baidu.com/map/sdk-ios.htm

對(duì)于想使用API開(kāi)發(fā)更豐富功能的開(kāi)發(fā)者話(huà),請(qǐng)參考SDK的類(lèi)文檔:

http://developer.baidu.com/map/ios_refer/html/annotated.html

此服務(wù)評(píng)測(cè)版權(quán)歸DevStore所有,禁止轉(zhuǎn)載,申請(qǐng)升級(jí)為特約評(píng)測(cè)員才可進(jìn)行測(cè)評(píng)立即申請(qǐng)

聲明:DevStore評(píng)測(cè)內(nèi)容都是基于專(zhuān)業(yè)評(píng)測(cè)人員/開(kāi)發(fā)者通過(guò)真實(shí)的測(cè)試之后得出的數(shù)據(jù),服務(wù)版本實(shí)時(shí)都在更新,所以評(píng)測(cè)并不一定

是此服務(wù)的最新版本,但我們會(huì)秉承公正專(zhuān)業(yè)精準(zhǔn)的態(tài)度,對(duì)開(kāi)發(fā)者負(fù)責(zé),同時(shí)歡迎大家監(jiān)督和建議,如對(duì)評(píng)測(cè)內(nèi)容有異議,請(qǐng)?zhí)峤患m錯(cuò),由專(zhuān)業(yè)的評(píng)測(cè)團(tuán)隊(duì)再次評(píng)

測(cè),我們會(huì)盡最大努力為大家提供更貼心的服務(wù)。

DevStore_全球首家第三方開(kāi)發(fā)者服務(wù)商店,最精準(zhǔn)的服務(wù)對(duì)比、最專(zhuān)業(yè)的服務(wù)評(píng)測(cè)、最及時(shí)的行業(yè)動(dòng)態(tài),為開(kāi)發(fā)者挑選服務(wù)提供最全面的參考和專(zhuān)業(yè)分析,加入DevStore,從此告別熬夜加班,你也可以這么帥!搜索微信號(hào):DevStore

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容