您好,登錄后才能下訂單哦!
最近仿照美團做了款應用,剛好用到百度地圖,高德地圖之前用的比較多,只是這個項目的后臺服務器是另外一個公司做的,他們用的就是百度地圖,現在網上用百度地圖的還不算太多,博文也是斷斷續續的,主要是中間跳躍有點大,沒有可運行的demo,看不到效果。糾結了好久,結合別人的總結和從百度官網上下載的demo,詳細記錄一下百度地圖的使用過程,分享一下,也便于以后查閱使用。
百度地圖的使用和蘋果一樣,相對于高德地圖來說規范了好多。直接將程序的bundle Identity和appkey值綁定,雖然剛開始用感覺有點不習慣,相信規范嚴謹對于國內的程序開發也算是一種引導吧。閑言少絮,下面開始百度地圖的使用說明。
第一步:注冊百度賬號,申請appkey。直接上圖片效果比較直接。,這里面有個小插曲,當初習慣性地將禁用服務ios地圖sdk勾選上,做了無數次的測試一直不顯示地圖頁面。糾結了好久,偶然間想到可能是appkey的問題,重新走了一遍,原來把禁用服務勾選上了,改過來就好了,做我們這行的嚴謹、認真還是很有必要地。
第二步:將百度地圖sdk包添加到程序,在AppDelegate導入
#import<CoreLocation/CoreLocation.h>
#import "AGViewDelegate.h"
#import "BMapKit.h"
上述文件,添加如下委托和對象
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,BMKGeneralDelegate>
{
AGViewDelegate *_viewDelegate;//
BMKMapManager* _mapManager;//百度地圖管理器
}
并在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中調用開啟百度地圖定位的方法
//地圖
- (void)configureAPIKey
{
_mapManager = [[BMKMapManager alloc] init];
BOOL ret = [_mapManager start:@"appkey"generalDelegate:self];
if (ret)
{
NSLog(@"百度地圖開啟定位:");
}
}
第三步:百度地圖視圖和百度位置服務的使用
在使用地圖的頁面導入百度地圖文件
#import "BMapKit.h"
添加
BMKMapViewDelegate,BMKLocationServiceDelegate委托對象
@property(nonatomic,retain)BMKMapView* baiduMapView;//地圖視圖
@property(nonatomic,retain)BMKLocationService* locationService;//百度地圖定位服務對象
@synthesize baiduMapView;
@synthesize locationService;
當然不用的時候需要釋放掉
if (baiduMapView)
{
[baiduMapView release];
baiduMapView = nil;
}
if (locationService)
{
[locationService release];
locationService = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
baiduMapView.delegate = self;
locationService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
[baiduMapView viewWillDisappear];
baiduMapView.delegate = nil;
locationService.delegate = nil;
[super viewWillDisappear:animated];
}
當然在使用地圖視圖控件之前需要先開啟百度地圖的定位服務
//初始化定位服務對象
self.locationService = [[[BMKLocationService alloc]init]autorelease];
locationService.delegate=self;
//顯示定位的藍點兒必須先開啟定位服務
[locationService startUserLocationService];
接下來就是地圖視圖控件使用了
self.baiduMapView = [[[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
//初始化百度地圖對象
baiduMapView.delegate = self;
[baiduMapView setMapType:BMKMapTypeStandard];
baiduMapView.zoomLevel=17;//地圖級別
//顯示定位的藍點兒必須先開啟定位服務
[locationService startUserLocationService];
[baiduMapView setShowsUserLocation:NO];
baiduMapView.userTrackingMode=BMKUserTrackingModeNone;//地圖模式
[baiduMapView setShowsUserLocation:YES];//顯示定位的藍點兒
coordinate.latitude = [XtomFunction xfuncGetAppdelegate].currentLocation.coordinate.latitude;//緯度
coordinate.longitude = [XtomFunction xfuncGetAppdelegate].currentLocation.coordinate.longitude;//經度
BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(0.3,0.3));
BMKCoordinateRegion adjustedRegion = [baiduMapView regionThatFits:viewRegion];
[baiduMapView setRegion:adjustedRegion animated:YES];
到這里已經可以展示地圖信息數據了,當然一些復雜的東西需要下面的委托實現
第四步:百度地圖中常用委托方法的使用
/**
*用戶方向更新后,會調用此函數
*@param userLocation 新的用戶位置
*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
[baiduMapView updateLocationData:userLocation];
NSLog(@"heading is %@,%f,%f,%f,%f,%f,%f",userLocation.heading,userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude,localLatitude,localLongitude,coordinate.latitude,coordinate.longitude);
NSLog(@"dingweifuwu:%f,%f",locationService.userLocation.location.coordinate.latitude,locationService.userLocation.location.coordinate.longitude);
NSLog(@"dangqian:%f,%f",locationService.userLocation.location.coordinate.latitude,locationService.userLocation.location.coordinate.longitude);
/*
//改變成百度經緯度
AppDelegate *myDelegate = [XtomFunction xfuncGetAppdelegate];
myDelegate.currentLocation = userLocation.location;
myDelegate.myCoordinate=CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);*/
}
//開始縮放委托
- (void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
{
NSString* showmeg = [NSString stringWithFormat:@"地圖區域發生了變化(x=%d,y=%d,\r\nwidth=%d,height=%d).\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d",(int)baiduMapView.visibleMapRect.origin.x,(int)baiduMapView.visibleMapRect.origin.y,(int)baiduMapView.visibleMapRect.size.width,(int)baiduMapView.visibleMapRect.size.height,(int)baiduMapView.zoomLevel,baiduMapView.rotation,baiduMapView.overlooking];
NSLog(@"gesturescale:%@",showmeg);
CLLocationCoordinate2D firstLocation=[baiduMapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:baiduMapView];
CLLocationCoordinate2D secondLocation=[baiduMapView convertPoint:CGPointMake(0, UI_View_Hieght+64) toCoordinateFromView:baiduMapView];
distance=[XtomFunction distanceBetweenOrderBy:firstLocation.latitude :firstLocation.longitude :secondLocation.latitude :secondLocation.longitude]/2;
//第一次獲取數據的時候不加載
if (isFirstLoad)
{
localLatitude=baiduMapView.centerCoordinate.latitude;
localLongitude=baiduMapView.centerCoordinate.longitude;
//重新獲取數據
[self requestGetLocationList];
}
}
//原理類似 UITableView 循環委托加載 CellforRowWithIndexPath
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
//商家詳情
if (isShangjia)
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]])
{
// 生成重用標示identifier
NSString *AnnotationViewID = @"shangjia";
// 檢查是否有重用的緩存
BMKAnnotationView* annotationView = [baiduMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
// 緩存沒有命中,自己構造一個,一般首次添加annotation代碼會運行到此處
if (annotationView == nil)
{ annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
// 設置重天上掉下的效果(annotation)
((BMKPinAnnotationView*)annotationView).animatesDrop = YES; }
// 設置位置
annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5)); annotationView.annotation = annotation;
// 單擊彈出泡泡,彈出泡泡前提annotation必須實現title屬性
annotationView.canShowCallout = YES;
//美食
if ([activityType isEqualToString:@"美食"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"美食圖標.png"];
}
//麗人
else if ([activityType isEqualToString:@"麗人"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"麗人圖標.png"];
}
//生活服務
else if ([activityType isEqualToString:@"生活服務"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"生活服務圖標.png"];
}
//休閑娛樂
else if ([activityType isEqualToString:@"休閑娛樂"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"休閑娛樂圖標.png"];
}
//旅游
else if ([activityType isEqualToString:@"旅游"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"旅游圖標.png"];
}
//購物
else if ([activityType isEqualToString:@"購物"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"購物圖標.png"];
}
//酒店
else if ([activityType isEqualToString:@"酒店"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"酒店圖標.png"];
}
//今日訂單
else if ([activityType isEqualToString:@"今日訂單"])
{
annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"今日訂單圖標.png"];
}
//annotationView.p_w_picpath=[UIImage p_w_picpathNamed:@"麗人圖標.png"];
// 設置是否可以拖拽
annotationView.draggable = NO;
return annotationView;
/*
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 設置該標注點動畫顯示
newAnnotationView.annotation=annotation;
newAnnotationView.p_w_picpath = [UIImage p_w_picpathNamed:@"麗人圖標.png"]; //把大頭針換成別的圖片
return newAnnotationView;*/
}
}
else if (isLuxian)
{
if ([annotation isKindOfClass:[RouteAnnotation class]])
{
return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation];
}
return nil;
}
}
這里介紹一下百度地圖中常見的兩個點,花費了我好長的時間總結出來的,分享一下吧。比如獲取百度地圖中當前的位置,可以在開啟百度位置服務,走完
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation委托方法后,通過
locationService.userLocation.location.coordinate獲得
再比如 想獲取當前地圖的中心點的經緯度 可以這樣獲得baiduMapView.centerCoordinate 還有百度地圖中坐標點和地圖中經緯度的轉換
CLLocationCoordinate2D firstLocation=[baiduMapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:baiduMapView];這個是遠點的經緯度
第四步:自定義標注的添加
//添加大頭釘
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = localLatitude;
coor.longitude = localLongitude;
annotation.coordinate = coor;
//annotation.title = @"這里是北京";
[baiduMapView addAnnotation:annotation];
結合
//原理類似 UITableView 循環委托加載 CellforRowWithIndexPath
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation委托方法實現,當然還有個獲取選中哪個大頭釘或者標注點的委托方法
//當選中一個annotation views時,調用此接口
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(CustomAnnotationView *)view
{
//只展示首頁的大頭釘
if (isLuxian||isShangjia)
{
return;
}
//獲取點擊視圖的區域
CLLocationCoordinate2D leftTopLocation=[baiduMapView convertPoint:CGPointMake(view.frame.origin.x, view.frame.origin.y) toCoordinateFromView:baiduMapView];
CLLocationCoordinate2D rightDownLocation=[baiduMapView convertPoint:CGPointMake(view.frame.origin.x+view.frame.size.width, view.frame.origin.y+view.frame.size.height) toCoordinateFromView:baiduMapView];
//臨時數組
NSMutableArray *tempArray=[[[NSMutableArray alloc]init]autorelease];
for (int i = 0; i<detailData.count; i++)
{
NSMutableDictionary *dict = [[[NSMutableDictionary alloc] init] autorelease];
NSDictionary *temDict = [detailData objectAtIndex:i];
for(NSString *key in temDict.allKeys)
{
NSString *value = [temDict objectForKey:key];
if(![XtomFunction xfunc_check_strEmpty:value])
{
[dict setObject:value forKey:key];
}
}
//緯度
float tempLat=[[temDict objectForKey:@"lat"]floatValue];
//經度
float tempLng=[[temDict objectForKey:@"lng"]floatValue];
NSLog(@"jingweidu:%f,%f,%f,%f,%f,%f",leftTopLocation.latitude,leftTopLocation.longitude,rightDownLocation.latitude,rightDownLocation.longitude,tempLat,tempLng);
//獲取該區域下的數據
if (tempLat >=rightDownLocation.latitude&&tempLat <=leftTopLocation.latitude&&tempLng >=leftTopLocation.longitude&&tempLng <=rightDownLocation.longitude)
{
[tempArray addObject:temDict];
}
}
NSLog(@"shuzu:%@",tempArray);
[view.superview insertSubview:view atIndex:1000];
view.temArr=tempArray;
view.biaozhu.dataSource=tempArray;
[view loadTableData];
[view retain];
}也一并分享一下吧。
第五步:百度地圖中公交線路信息的獲取
公交、地鐵、汽車、步行這幾個是常見的東西這個比較麻煩一些,感興趣的可以去百度地圖的demo中查詢,我給出鏈接吧。http://download.csdn.net/detail/kuuailetianzi/7907889 至此,百度地圖的使用介紹的差不多了,我也該休息休息了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。