IOS百度地圖API開發自定義氣泡,點擊氣泡自動生成路線,以及拖拽
IOS百度地圖開發POISearch搜索附近停車場,附近加油站
IOS百度地圖視角跳到用戶當前位置
IOS百度地圖開發實時路況
IOS開發百度地圖自動導航
IOS開發百度地圖在大頭釘上加文字和圖片,點擊這個總體再彈出氣泡
IOS開發百度地圖實現經緯度導航,無需地名。
ios
百度地圖零基礎到各類效果界面。上面的幾行不用看,那是爲了SEO,就是爲了讓我這篇博客讓更多的人搜索到,上面的問題我都已經解決,都在文章裏面。下面是步驟:
1.申請百度地圖key。2013年10月以後的2.1.0版本開始啓用新的key!要想使用百度地圖,你須要申請key,這個key是安桌蘋果通用,可是你的項目名必須跟你的key吻合,至於什麼是項目名,很少說,到網址http://lbsyun.baidu.com/apiconsole/key申請,git
應用Id | 應用名稱 | 訪問應用(ak) | 應用類別 | 備註信息(雙擊更改) | 應用配置 |
---|---|---|---|---|---|
系統默認AK | 系統默認AK |
AA1********28e
|
未配置 | 當前系統所用AK | 當前系統所用AK |
17673 | IphoneMapSdkDemo | pytxh1exmzkAneO5Vf4K | 移動端 | 設置 刪除 | |
17721 | MapTest | LqwUseZROjwxwb2zZ4e | 移動端 | 設置 刪除 |
2.下載百度開發包,http://api.map.baidu.com/lbsapi/cloud/sdkiosdev-download.htm,
3.建項目。這部分設計添加lib以及資源文件,參考百度的IOS api開發指南作就行,那些內容已經在2013年10月份更新過了,別去看網上亂七八糟的帖子,照着裏面作就能順利經過編譯。有帖子說那些方法過期,錯!
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上運行這個百度地圖,會出現-[UIDevice uniqueGlobalDeviceIdentifier]: unrecognized selector sent to instance 0x1ed19370
這麼一個bug,解決辦法不少,可是我感受最爽的一種辦法就是加入4個文件NSString+MD5Addition,UIDevice+IdentifierAddition,直接加入到項目裏面就能夠,無需引入頭文件,下載地址http://www.kuaipan.cn/file/id_30491149655344975.htm
7.在viewController.mm中的viewDidLoad改成以下代碼
- (void)viewDidLoad
{
[super viewDidLoad];
BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 548)];
self.view = mapView;
// Do any additional setup after loading the view, typically from a nib.
}
保存後運行,一個最簡單的百度地圖API應用就完成了
源代碼下載http://www.kuaipan.cn/file/id_30491149655344976.htm
百度地圖的功能很強大,上面只是牛刀小試
api
功能五,搜索,用的很普遍,這個功能很強大,你能夠搜索某一座標點附近的加油站,停車場,賓館,酒店,餐廳,甚至廁所都能搜索到,好比說你想查找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)type errorCode:(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 = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
}
清除地圖上全部痕跡和路線
-(void)clereOldYJDH{
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
[self Region];
}
//當選中一個annotation views時,調用此接口
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
NSLog(@"選中一個annotation views:%f,%f",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
}
//當取消選中一個annotation views時,調用此接口
- (void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{
NSLog(@"取消選中一個annotation views");
}
//當mapView新添加annotation views時,調用此接口
- (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
NSLog(@"mapView新添加annotation views");
}
//當點擊annotation view彈出的泡泡時,調用此接口
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view
{
NSLog(@"點擊annotation view彈出的泡泡");
}
//拖動annotation view時view的狀態變化
- (void)mapView:(BMKMapView *)mapView annotationView:(BMKAnnotationView *)view didChangeDragState:(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 *)view viewForAnnotation:(id <BMKAnnotation>)annotation
在這個委託中實現以下代碼
UIView *viewForImage=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 132, 64)];
UIImageView *imageview=[[UIImageView alloc]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.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
在網上發現別人專門創建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];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays: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:nil endNode:end];
if (!flag) {
NSLog(@"search failed");
}
[start release];
[end release];
[self Region];
}iphone
相關文章:ide