關于怎么導入百度地圖SDK與創(chuàng)建應用就不多說了诊胞,百度的文檔應該比我說的更詳細沙峻,下面直接正文吧
1.首先地圖的初始化
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
/// 地圖
- (void)setupView {
_mapView = [[BMKMapView alloc]init];
[_mapView setMapType:BMKMapTypeStandard];// 地圖類型 ->衛(wèi)星/標準睦授、
_mapView.showsUserLocation = YES;
_mapView.gesturesEnabled = YES;
_mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內存的釋放
_mapView.frame = self.view.bounds;
[self.view addSubview:_mapView];
}
// 地圖的初始化
- (void)setupMapService {
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
_locService.desiredAccuracy =? kCLLocationAccuracyBest;
_locService.distanceFilter = 10;//大于100米
[_locService startUserLocationService];
_geoCodeSerch = [[BMKGeoCodeSearch alloc] init];
_geoCodeSerch.delegate = self;
_mapView.showsUserLocation = NO;//先關閉顯示的定位圖層
_mapView.userTrackingMode = BMKUserTrackingModeFollow;//設置定位的狀態(tài)
_mapView.showsUserLocation = YES;//顯示定位圖層
_clusterManager = [[BMKClusterManager alloc] init];
//初始化檢索對象
self.districtSearch = [[BMKDistrictSearch alloc] init];
//設置delegate专酗,用于接收檢索結果
self.districtSearch.delegate = self;
//在此處理正常結果
_clusterCaches = [[NSMutableArray alloc] init];
for (NSInteger i = 3; i < 22; i++) {
[_clusterCaches addObject:[NSMutableArray array]];
? ? ?}
}
2.地圖的開始與加載完畢
/**
*地圖初始化完畢時會調用此接口
*@param mapView 地圖View
*/
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
displayParam.isAccuracyCircleShow = NO;//精度圈是否顯示
[_mapView updateLocationViewWithParam:displayParam];
BMKCoordinateRegion region ;//表示范圍的結構體
region.center = _mapView.centerCoordinate;//中心點
self.cCoordinate = _mapView.centerCoordinate;//中心點
region.span.latitudeDelta = 0.002;//經度范圍(設置為0.1表示顯示范圍為0.2的緯度范圍)
region.span.longitudeDelta = 0.002;//緯度范圍
[_mapView setRegion:region animated:YES];
[self updateClusters];
}
/**
*地圖渲染每一幀畫面過程中睹逃,以及每次需要重繪地圖時(例如添加覆蓋物)都會調用此接口
*@param mapView 地圖View
*@param status 此時地圖的狀態(tài)
*/
- (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status {
if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel) {
? ?[self updateClusters];
? }
}
3.地圖的位置發(fā)生變化
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//屏幕坐標轉地圖經緯度
CLLocationCoordinate2D MapCoordinate = [_mapView convertPoint:_mapView.center toCoordinateFromView:_mapView];
if (_reverseGeoCodeOption==nil) {
//初始化反地理編碼類
_reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
}
//需要逆地理編碼的坐標位置
_reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;
[_geoCodeSerch reverseGeoCode:_reverseGeoCodeOption];
// 如果你是請求自己后臺的數據可以在這里請求,可以省去下面檢索回來的數據祷肯,由于我的是demo沉填,所以下面是必須要的
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
option.pageIndex = 1;
option.pageCapacity = 10;
option.location = mapView.centerCoordinate;
option.keyword = @"小吃";
BOOL flag = [self.poiSearch poiSearchNearBy:option];
? if(flag) {
? ? ? ? NSLog(@"周邊檢索發(fā)送成功");
? ? ?}else{
? ? ? ?NSLog(@"周邊檢索發(fā)送失敗");
? ? }
}
// 當地圖發(fā)生改變之后,檢索并
//實現PoiSearchDeleage處理回調結果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
//在此處理正常結果
}
else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
//當在設置城市未找到結果佑笋,但在其他城市找到結果時翼闹,回調建議檢索城市列表
// result.cityList;
NSLog(@"起始點有歧義");
} else {
NSLog(@"抱歉,未找到結果");
}
// 清空緩存數據
?[_clusterManager clearClusterItems];
?for (BMKPoiInfo *poiInfo in poiResultList.poiInfoList) {
? XJCluster *cluster = [[XJCluster alloc] init];
? cluster.name = poiInfo.name;
? ?cluster.pt = poiInfo.pt;
? ? // 添加數據
? ?[self addAnnoWithPT:cluster];
? ?}
}
4.更新點聚合狀態(tài)
- (void)updateClusters {
_clusterZoom = (NSInteger)_mapView.zoomLevel;
@synchronized(_clusterCaches) {
NSMutableArray *clusters = [NSMutableArray array];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
///獲取聚合后的標注
__block NSArray *array = [_clusterManager getClusters:_clusterZoom];
dispatch_async(dispatch_get_main_queue(), ^{
for (BMKCluster *item in array) {
XJClusterAnnotation *annotation = [[XJClusterAnnotation alloc] init];
annotation.coordinate = item.coordinate;
annotation.size = item.size;
annotation.title = item.title;
annotation.cluster = item.cluster;
[clusters addObject:annotation];
}
[_mapView removeOverlays:_mapView.overlays];
[_mapView removeAnnotations:_mapView.annotations];
[_mapView addAnnotations:clusters];
});
});
}
}
demo:https://github.com/SingGitHub/BMKMapClusterView