百度地圖官網: http://developer.baidu.com/map/index.php?title=iossdkphp
百度地圖集成ios
1.引入相關包git
注:不須要使用地圖功能的能夠不用boundle包,模擬器使用的framework和真機不同具體看官網緩存
2.info.plist中網絡
3.在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。app
4.將一個文件設置爲.mmide
百度地圖使用ui
1.AppDelegate中註冊編碼
_mapManager = [[BMKMapManager alloc]init]; // 若是要關注網絡及受權驗證事件,請設定 generalDelegate參數 BOOL ret = [_mapManager start:@"Y1TKLraxiftqGsewOzLeRVFb" generalDelegate:self]; if (!ret) { NSLog(@"manager start failed!"); }
2.使用百度地圖spa
BMKMapView *_mapView; //地圖 BMKPoiSearch *_poisearch; //poi搜索 BMKGeoCodeSearch *_geocodesearch; //geo搜索服務
3.代碼
// // ViewController.m // baidumapTest // // Created by apple on 15/8/26. // Copyright (c) 2015年 tqh. All rights reserved. // //使用百度地圖定位,poi搜索,地理編碼功能 #import "ViewController.h" #import "WJBaiduMapTools.h" @interface ViewController ()<BMKPoiSearchDelegate,BMKMapViewDelegate,BMKGeoCodeSearchDelegate>{ BMKMapView *_mapView; //地圖 BMKPoiSearch *_poisearch; //poi搜索 BMKGeoCodeSearch *_geocodesearch; //geo搜索服務 } @end @implementation ViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } -(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用時,置nil _poisearch.delegate = nil; // 不用時,置nil } - (void)viewDidLoad { [super viewDidLoad]; _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; _mapView.delegate = self; [self.view addSubview:_mapView]; [_mapView setZoomLevel:13]; _mapView.isSelectedAnnotationViewFront = YES; //延遲搜索 [self performSelector:@selector(delay) withObject:self afterDelay:2]; // WJBaiduMapTools *tool = [WJBaiduMapTools instance]; // [tool startlocation:YES locationSuccess:^(double longitude, double latitude) { // NSLog(@"%f,%f",longitude,latitude); // } addressSuccess:^(double longitude, double latitude, BMKAddressComponent *address) { // NSLog(@"%f,%f",longitude,latitude); // NSLog(@"%@ %@",address.city,address.streetName); // }]; // } - (void)delay { [self nameSearch]; } #pragma mark - poi搜索 - (void)citySearch { _poisearch = [[BMKPoiSearch alloc]init]; _poisearch.delegate = self; BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init]; citySearchOption.pageIndex = 0; citySearchOption.pageCapacity = 20; citySearchOption.city= @"成都"; citySearchOption.keyword = @"餐廳"; BOOL flag = [_poisearch poiSearchInCity:citySearchOption]; if(flag) { NSLog(@"城市內檢索發送成功"); } else { NSLog(@"城市內檢索發送失敗"); } } #pragma mark - 根據名稱搜索 - (void)nameSearch { _geocodesearch = [[BMKGeoCodeSearch alloc]init]; _geocodesearch.delegate = self; // 此處記得不用的時候須要置nil,不然影響內存的釋放 BMKGeoCodeSearchOption *geocodeSearchOption = [[BMKGeoCodeSearchOption alloc]init]; geocodeSearchOption.city= @"成都"; geocodeSearchOption.address = @"武侯祠"; BOOL flag = [_geocodesearch geoCode:geocodeSearchOption]; if(flag) { NSLog(@"geo檢索發送成功"); } else { NSLog(@"geo檢索發送失敗"); } } #pragma mark - 根據經緯度搜索 - (void)reverseGeoPointSearch { _geocodesearch = [[BMKGeoCodeSearch alloc]init]; _geocodesearch.delegate = self; // 此處記得不用的時候須要置nil,不然影響內存的釋放 CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0}; NSString *longitude = @"116.403981"; NSString *latitude = @"39.915101"; if (latitude != nil && longitude != nil) { pt = (CLLocationCoordinate2D){[latitude floatValue], [longitude floatValue]}; } BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = pt; BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; if(flag) { NSLog(@"反geo檢索發送成功"); } else { NSLog(@"反geo檢索發送失敗"); } } #pragma mark - BMKMapViewDelegate //長按獲取經緯度,並添加標註 - (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate { NSLog(@"經緯度:%f,%f",coordinate.longitude,coordinate.latitude); BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init]; item.coordinate = coordinate; item.title = @"哇哈哈"; [_mapView addAnnotation:item]; } - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation { // 生成重用標示identifier NSString *AnnotationViewID = @"xidanMark"; // 檢查是否有重用的緩存 BMKAnnotationView* annotationView = [view 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; // 設置是否能夠拖拽 annotationView.draggable = NO; return annotationView; } - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view { [mapView bringSubviewToFront:view]; [mapView setNeedsDisplay]; } - (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views { NSLog(@"didAddAnnotationViews"); } #pragma mark - BMKPoiSearchDelegate - (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode { // 清楚屏幕中全部的annotation NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; if (errorCode == BMK_SEARCH_NO_ERROR) { NSLog(@"正常返回"); NSMutableArray *annotations = [NSMutableArray array]; for (int i = 0; i < poiResult.poiInfoList.count; i++) { BMKPoiInfo* poi = [poiResult.poiInfoList objectAtIndex:i]; BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init]; item.coordinate = poi.pt; item.title = poi.name; NSLog(@"%@",poi.name); [annotations addObject:item]; } [_mapView addAnnotations:annotations]; [_mapView showAnnotations:annotations animated:YES]; } else if (errorCode == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){ NSLog(@"起始點有歧義"); } else if (errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_NOT_SUPPORT_BUS){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_NOT_SUPPORT_BUS_2CITY){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_RESULT_NOT_FOUND){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_ST_EN_TOO_NEAR){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_KEY_ERROR){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_NETWOKR_ERROR){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_NETWOKR_TIMEOUT){ // 各類狀況的判斷。。。 NSLog(@"其餘狀況"); } else if (errorCode == BMK_SEARCH_PERMISSION_UNFINISHED){ // 各類狀況的判斷。。。 NSLog(@"還未完成鑑權,請在鑑權經過後重試"); }else { NSLog(@"不知道了"); } } #pragma mark - BMKGeoCodeSearchDelegate // *返回地址信息搜索結果 - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == 0) { BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init]; item.coordinate = result.location; item.title = result.address; [_mapView addAnnotation:item]; _mapView.centerCoordinate = result.location; NSString* titleStr; NSString* showmeg; titleStr = @"正向地理編碼"; showmeg = [NSString stringWithFormat:@"經度:%f,緯度:%f",item.coordinate.latitude,item.coordinate.longitude]; UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"肯定",nil]; [myAlertView show]; //應該在這裏獲得經緯度而後 } } // *返回反地理編碼搜索結果 - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { NSArray* array = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:array]; array = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:array]; if (error == 0) { BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init]; item.coordinate = result.location; item.title = result.address; [_mapView addAnnotation:item]; _mapView.centerCoordinate = result.location; NSString* titleStr; NSString* showmeg; titleStr = @"反向地理編碼"; showmeg = [NSString stringWithFormat:@"%@",item.title]; UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:titleStr message:showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"肯定",nil]; [myAlertView show]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
具體代碼最下面附上Demo也能夠去看百度官方Demo
注意:使用模擬器運行的時候,須要設置它的經緯度
以下圖:
百度地圖經常使用功能Demo連接:(我用的是模擬器,用真機的童鞋要記得把包換了哦)
http://pan.baidu.com/s/1dDtuZpZ
注意:若是百度地圖不顯示,只有網格有幾種狀況
1,你的程序的bundle id和百度官網上註冊生成的key不一樣
2.你沒有導入地圖資源文件
3.你沒有啓動百度地圖引擎