原地址:http://blog.sina.com.cn/s/blog_68661bd80101k4rx.html
IOS百度地圖API開發自定義氣泡,點擊氣泡自動生成路線,以及拖拽
IOS百度地圖開發POISearch搜索附近停車場,附近加油站IOS百度地圖視角跳到用戶當前位置
IOS百度地圖開發實時路況
IOS開發百度地圖自動導航
IOS開發百度地圖在大頭釘上加文字和圖片,點擊這個整體再彈出氣泡
IOS開發百度地圖實現經緯度導航,無需地名。
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
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
這個代碼是真機運行有效。模擬器會出錯,
百度地圖的功能很強大,上面只是牛刀小試
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
部分效果圖
-(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.在其他軟件中打開百度地圖,並在百度軟件中實現導航,搜索等。
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];
}
B6258A4A5FFEE50A734B7FAACF104831A18C177A