IOS開發百度地圖API-用點生成路線,導航,氣泡響應

原地址:http://blog.sina.com.cn/s/blog_68661bd80101k4rx.html


IOS百度地圖API開發自定義氣泡,點擊氣泡自動生成路線,以及拖拽

IOS百度地圖開發POISearch搜索附近停車場,附近加油站
IOS百度地圖視角跳到用戶當前位置
IOS百度地圖開發實時路況
IOS開發百度地圖自動導航
IOS開發百度地圖在大頭釘上加文字和圖片,點擊這個整體再彈出氣泡
IOS開發百度地圖實現經緯度導航,無需地名。

 第一部分--牛刀小試
百度地圖零基礎到各種效果界面。上面的幾行不用看,那是爲了SEO,就是爲了讓我這篇博客讓更多的人搜索到,上面的問題我都已經解決,有問題歡迎和我交流。我也會持續更新兄弟們的問題。下面是步驟:

1.申請百度地圖key,要想使用百度地圖,你需要申請key,這個key是安桌蘋果通用,但是你的項目名必須跟你的key吻合,至於什麼是項目名,不多說,到網址http://api.map.baidu.com/lbsapi/cloud/ios-mobile-apply-key.htm申請,







2.下載百度開發包http://api.map.baidu.com/lbsapi/cloud/sdkiosdev-download.htm
3.建立一個項目,項目名爲cscapp,然後加入框架,在1.X的版本中只要前兩個,在2.X的版本中可能就是下面4個,以後說不定更多:MapKit.framework,CoreLocation.framework,QuartzCore.framework,SystemConfiguration.framework將下載的開發包inc,libbaidumapapi.a,mapapi.bundle這三個文件加入到程序中
4.將ViewController的.m改成.mm
5.在委託中.h文件中中加入#import"BMapKit.h",並聲明變量BMKMapManager*_mapManager;在.m委託中加入
_  mapManager = [[BMKMapManager alloc]init];
    //如果要關注網絡及授權驗證事件,請設定 generalDelegate 參數
    BOOL ret =[_mapManager start:@"3102732B30E0D66EF51415C9E6CE055EC78FF07E"generalDelegate:nil];
    if (!ret){
       NSLog(@"manager start failed!");
    }
6.注意:在這裏就開始運行程序的話,我在我的iphone上運行這個百度地圖,會出現-[UIDeviceuniqueGlobalDeviceIdentifier]: unrecognized selector sent toinstance 0x1ed19370
這麼一個bug,解決辦法很多,但是我感覺最爽的一種辦法就是加入4個文件NSString+MD5Addition,UIDevice+IdentifierAddition,直接加入到項目裏面就可以,無需引入頭文件,下載地址
http://www.kuaipan.cn/file/id_30491149655344975.htm
這是蘋果SDK與百度SDK版本沒同步引起的一個BUG,如果你運行程序,沒有問題,就不要加入這些文件。
7.在viewController.mm中的viewDidLoad改爲如下代碼
- (void)viewDidLoad
{
    [superviewDidLoad];
    BMKMapView*mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320,548)];
    self.view =mapView;
    // Do anyadditional setup after loading the view, typically from anib.
}
保存後運行,一個最簡單的百度地圖API應用就完成了
源代碼下載http://www.kuaipan.cn/file/id_30491149655344976.htm
這個代碼是真機運行有效。模擬器會出錯,
百度地圖的功能很強大,上面只是牛刀小試


第二部分
    在開始之前,要對座標有個瞭解,手機自帶GPS定位功能,獲得的GPS座標無法直接在國內地圖中直接使用,因爲經過了偏移轉換,目的是爲了國家安全,假如小日本知道全中國任意點的精確座標,可以把國內的任意地圖資源整合的話,那麼它的導彈想打到哪就是哪。當然這只是一方面。
    還有新手注意,別在軟件中使用多個地圖,也不要同一界面中同一地圖的多個窗口。地圖授權是根據項目名,那麼在項目中需要多處使用的話,要使用單例模式。如果在多個界面加載了地圖界面,各個界面由於是使用單例模式會相互干擾,所以可以根據需要進行重定位。

功能五,搜索,用的很廣泛,這個功能很強大,你可以搜索某一座標點附近的加油站,停車場,賓館,酒店,餐廳,甚至廁所都能搜索到,比如說你想查找5000米內的加油站,以及1000米內的停車場。
flag = [_search poiSearchNearBy:@"加油站" center:coor1 radius:5000 pageIndex:0];
flag = [_search poiSearchNearBy:@"停車場" center:coor1 radius:1000 pageIndex:0];
然後用委託-(void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)typeerrorCode:(int)error
輸出結果
將百度地圖視角切換到某一座標點
-(void)Region{
   CLLocationCoordinate2D coor;
   coor.latitude = self._latitude;
   coor.longitude = self._longitude;
    NSDictionary*tip =  BMKBaiduCoorForWgs84(coor);
   CLLocationCoordinate2D coor1= BMKCoorDictionaryDecode(tip);
   BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(coor1,BMKCoordinateSpanMake(0.05,0.05));
   BMKCoordinateRegion adjustedRegion = [_mapViewregionThatFits:viewRegion];
    [_mapViewsetRegion:adjustedRegion animated:YES];
}
清除地圖上所有痕跡和路線
-(void)clereOldYJDH{
    NSArray*array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapViewremoveAnnotations:array];
    array =[NSArray arrayWithArray:_mapView.overlays];
    [_mapViewremoveOverlays:array];
    [selfRegion];
}
//當選中一個annotationviews時,調用此接口
- (void)mapView:(BMKMapView *)mapViewdidSelectAnnotationView:(BMKAnnotationView *)view
{
   NSLog(@"選中一個annotationviews:%f,%f",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
}
//當取消選中一個annotationviews時,調用此接口
- (void)mapView:(BMKMapView *)mapViewdidDeselectAnnotationView:(BMKAnnotationView *)view{
   NSLog(@"取消選中一個annotation views");
}
//當mapView新添加annotationviews時,調用此接口
- (void)mapView:(BMKMapView *)mapViewdidAddAnnotationViews:(NSArray *)views
{
   NSLog(@"mapView新添加annotation views");
}
//當點擊annotationview彈出的泡泡時,調用此接口
- (void)mapView:(BMKMapView *)mapViewannotationViewForBubble:(BMKAnnotationView *)view
{
   NSLog(@"點擊annotation view彈出的泡泡");
}
//拖動annotationview時view的狀態變化
- (void)mapView:(BMKMapView *)mapViewannotationView:(BMKAnnotationView *)viewdidChangeDragState:(BMKAnnotationViewDragState)newStatefromOldState:(BMKAnnotationViewDragState)oldState
{
   NSLog(@"動annotation view時view的狀態變化");
}
//標註呈綠色樣式大頭釘
((BMKPinAnnotationView *)annotationView).pinColor =BMKPinAnnotationColorGreen;
//允許用戶拖動
[annotationView setDraggable:YES];
//氣泡框左側顯示的View,可自定義
annotationView.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_location.png"]];
//氣泡框右側顯示的View 可自定義
annotationView.rightCalloutAccessoryView =selectButton;
//讓標註在進入界面時就處於彈出氣泡框的狀態
[annotationView setSelected:YES animated:YES];
//整個標註的偏移量
annotationView.centerOffset = CGPointMake(0,-(annotationView.frame.size.height * 0.5));
annotationView.annotation = annotation;//綁定對應的標點經緯度
annotationView.canShowCallout = TRUE;//允許點擊彈出氣泡框


在地圖上定製標註替代大頭釘,可以將文字圖片所有能加到view中的,都可以以大頭釘的形式顯示出來,需要將view轉換爲image主要代碼,最重要的是知道這個原理,然後實現起來就很簡單:
- (BMKAnnotationView *)mapView:(BMKMapView *)viewviewForAnnotation:(id )annotation
在這個委託中實現如下代碼
      UIView *viewForImage=[[UIView alloc]initWithFrame:CGRectMake(0, 0,132, 64)];
       UIImageView *imageview=[[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 32, 64)];
       [imageview setImage:[UIImage imageNamed:@"車位置.png"]];
       [viewForImage addSubview:imageview];
       
       UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(32, 0, 100,64)];
       label.text=@"陳雙超";
       label.backgroundColor=[UIColor clearColor];
       [viewForImage addSubview:label];
       annotationView.image=[self getImageFromView:viewForImage];


-(UIImage *)getImageFromView:(UIView *)view{
   UIGraphicsBeginImageContext(view.bounds.size);
    [view.layerrenderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
    returnimage;
}

在網上發現別人專門建立view來定製氣泡和專門建立文件來定製大頭釘,覺得那方法對代碼管理起來更簡單,可讀性強。值得學習http://www.kuaipan.cn/file/id_30491149655345085.htm
@interface KYBubbleView : UIScrollView{   //UIView是氣泡view的本質
    NSDictionary*_infoDict;
   UILabel        *titleLabel;//標題label
   UILabel        *detailLabel;//副標題label
   UILabel        *contactLabel;   //聯繫人
   UILabel        *homeAddresslabel;   //家庭地址
   UIButton       *rightButton;
   NSUInteger     index;
}
#import "BMKPointAnnotation.h"

@interface KYPointAnnotation : BMKPointAnnotation {
    NSUInteger_tag;
}

@property NSUInteger tag;

@end
 

部分效果圖









第三部分
1.有一個朋友問我他做導航時,傳參數遇到問題,就是輸入漢字部分沒有,僅靠兩點的經緯度怎麼實現路線導航,他試了很久沒弄出來。然後我把解決這個問題的源碼貼出來。我做的是駕車路線,如果需要步行或者公交則需要作調整。主要涉及的方法如下,實現這個的代碼,我直接從我的項目中弄出來的http://www.kuaipan.cn/file/id_30491149655345069.htm
-(void)onClickDriveSearch
{
   NSLog(@"%f,%f,%f,%f",_startCoordainateXText,_startCoordainateYText,_endCoordainateXText,_endCoordainateYText);
    count =0;
   isLoadingMap=2;

   if(!_endCoordainateXText ||!_endCoordainateYText)isLoadingMap=1;
   
    NSArray*array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapViewremoveAnnotations:array];
    array =[NSArray arrayWithArray:_mapView.overlays];
    [_mapViewremoveOverlays:array];
   CLLocationCoordinate2D startPt = (CLLocationCoordinate2D){0,0};
   CLLocationCoordinate2D endPt = (CLLocationCoordinate2D){0,0};
    if(_startCoordainateXText && _startCoordainateYText ) {
       startPt =(CLLocationCoordinate2D){_startCoordainateYText,_startCoordainateXText };
    }
    if(_endCoordainateYText  &&_endCoordainateXText ) {
       endPt =(CLLocationCoordinate2D){ _endCoordainateYText,_endCoordainateXText};
    }
    BMKPlanNode*start = [[BMKPlanNode alloc]init];
    start.pt =startPt;
    start.name =nil;
   
    BMKPlanNode*end = [[BMKPlanNode alloc]init];
    end.pt =endPt;
    end.name =nil;
   
    BOOL flag =[_search drivingSearch:nil startNode:start endCity:nilendNode:end];
    if (!flag){
      NSLog(@"search failed");
    }
    [startrelease];
    [endrelease];
    [selfRegion];
}

  2.在其他軟件中打開百度地圖,並在百度軟件中實現導航,搜索等
     在這裏僅僅實現打開百度地圖,如果打不開,則跳到App Store下載百度地圖軟件。
      判斷能否打開URL,能打開就打開,不能打開就跳轉到百度地圖,可能有的同學要做一打開百度地圖就導航,或者顯示搜索到的關鍵字的結果。這個就是改下下面代碼中的的stringURL,帶什麼參數,需要對照百度協議,打開http://developer.baidu.com/map/uri-introios.htm對照協議。
NSString*stringURL= [NSStringstringWithFormat:@"mapbarnaviiap:"];

          NSURL *url = [NSURL URLWithString:stringURL];

          if ([[UIApplication sharedApplication] canOpenURL:url]) {

              [[UIApplication sharedApplication] openURL:url];

          }else{

              UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示"message:@"您未安裝百度地圖,是否立即前往APPStore安裝!"delegate:selfcancelButtonTitle:nilotherButtonTitles:@"立即安裝",@"稍後安裝",nil];

              [alert show];

              [alert release];

           }

     

3.爲什麼地圖打開是空的,只有網格?包括百度提供的demo也是空的,原因很簡單,百度折騰的。


問題在於申請的key。
之前申請可能不需要完整的簽名,現在需要完整的簽名才行。簡單點說,之前申請叫cscapp就ok。現在申請需要com.calinks.cscapp。這個名字必須跟Bundle Identifer完全相同才能通過百度祕要。

        B6258A4A5FFEE50A734B7FAACF104831A18C177A

           tie4cZmrsLkUAdjpOGFvQHfG
讓我糾結的事情又發生了,用上面的key,百度地圖提示keyStatus =40002,百度再次更新key的管理機制,我只想說:坑爹~!。新key註冊地址http://lbsyun.baidu.com/apiconsole/key
         而且百度不提供老版本sdk下載了,只能下載矢量地圖。但是老版本的sdk依然可以使用。


4.很久沒弄百度地圖了,計算導航路線的時間和里程,用的2.2.0最新百度SDK做的,無聊的可以看看,沒什麼用,能運行。http://www.kuaipan.cn/share.htm?s=FEQCeLtLUCpxSrjG-AGxTigAC(oU=

5.已經有幾個人問我這個問題了,一般是剛接觸百度地圖的,這個問題是因爲在模擬器運行導致,添加的sdk沒有包含模擬器的指令集。用真機運行應該是沒有問題的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章