這段時間,在作百度地圖的開發過程當中,會遇到一些小問題。作點記錄。php
開發前期,須要兩個連接:ios
申請密鑰:http://lbsyun.baidu.com/index.php?title=iossdk/guide/keyapi
配置開發環境:http://lbsyun.baidu.com/index.php?title=iossdk/guide/buildprojectapp
申請到密鑰的時候,要注意BundleID和Xcode的一致。ide
使用效果:函數
爲了減小頁面的資源,在主要頁面裏,只放一張靜態圖,經過調用百度的靜態API生成。用戶須要查看詳情的時候,再跳到地圖的導航。ui
生成靜態圖API地址:http://lbsyun.baidu.com/index.php?title=static編碼
NSString *strCerter = [NSString stringWithFormat:@"%@,%@",_houseModel.map.lng,_houseModel.map.lat]; NSString *mapUrl = [NSString stringWithFormat:@"http://api.map.baidu.com/staticimage?center=%@&width=%f&height=%@&zoom=11&markers=%@&markerStyles=l,A",strCerter,RFSCREEN_W,@"200",strCerter]; UIImageView *mapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, RFSCREEN_W, 200)]; [mapImageView sd_setImageWithURL:[NSURL URLWithString:mapUrl]]; [cell addSubview:mapImageView];
轉跳到MapController就很簡單了,代碼如:atom
// // RFHouseMapViewController.m // RFHouse // // Created by jiangys on 16/3/29. // Copyright © 2016年 Jiangys. All rights reserved. // #import "RFHouseMapViewController.h" #import <BaiduMapAPI_Utils/BMKUtilsComponent.h> #import <BaiduMapAPI_Map/BMKMapComponent.h> #import <BaiduMapAPI_Search/BMKSearchComponent.h> @interface RFHouseMapViewController ()<BMKMapViewDelegate, BMKGeoCodeSearchDelegate,BMKGeneralDelegate> /** 百度地圖 */ @property (nonatomic, strong) BMKMapView *mapView; @property (nonatomic, strong) BMKGeoCodeSearch *geocodesearch; @property (nonatomic, strong) BMKMapManager *mapManager; @end @implementation RFHouseMapViewController #pragma mark -- 生命週期 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = Color_White; // 要使用百度地圖,請先啓動BaiduMapManager _mapManager = [[BMKMapManager alloc]init]; BOOL ret = [_mapManager start:@"rUvg0vdlCsdI227Z6I1LoV8xGhZ0ZZwb" generalDelegate:self]; if (!ret) { NSLog(@"manager start failed!"); } // 初始化地圖 _geocodesearch = [[BMKGeoCodeSearch alloc]init]; _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, RFSCREEN_W, RFSCREEN_H)]; [_mapView setZoomLevel:14]; [self.view addSubview:_mapView]; [self reverseGeocodeWithLng:_lng lat:_lat]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewWillAppear:(BOOL)animated { [_mapView viewWillAppear]; _mapView.delegate = self; // 此處記得不用的時候須要置nil,不然影響內存的釋放 _geocodesearch.delegate = self; // 此處記得不用的時候須要置nil,不然影響內存的釋放 } -(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用時,置nil _geocodesearch.delegate = nil; // 不用時,置nil } - (void)dealloc { if (_geocodesearch != nil) { _geocodesearch = nil; } if (_mapView) { _mapView = nil; } } #pragma mark 百度地圖API // 百度地圖:反向geo檢索 -(void)reverseGeocodeWithLng:(CGFloat)lng lat:(CGFloat)lat { CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0}; pt = (CLLocationCoordinate2D){lat, lng}; BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = pt; BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; if(flag) { RFLog(@"反geo檢索發送成功"); } else { RFLog(@"反geo檢索發送失敗"); } } // 百度地圖:根據anntation生成對應的View - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation { NSString *AnnotationViewID = @"annotationViewID"; //根據指定標識查找一個可被複用的標註View,通常在delegate中使用,用此函數來代替新申請一個View BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed; ((BMKPinAnnotationView*)annotationView).animatesDrop = YES; } annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5)); annotationView.annotation = annotation; annotationView.canShowCallout = TRUE; return annotationView; } // 百度地圖:反向地址編碼 -(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; } } - (void)onGetNetworkState:(int)iError { if (0 == iError) { NSLog(@"聯網成功"); } else{ NSLog(@"onGetNetworkState %d",iError); } } - (void)onGetPermissionState:(int)iError { if (0 == iError) { NSLog(@"受權成功"); } else { NSLog(@"onGetPermissionState %d",iError); } } @end
要注意Start這裏填的是百度的"百度地圖iOS SDK開發密鑰"spa
BOOL ret = [_mapManager start:@"rUvg0vdlCsdI227Z6I1LoV8xGhZ0ZZwb" generalDelegate:self];