iOS-百度地圖點(diǎn)聚合與自定義針頭

示例圖

關(guān)于怎么導(dǎo)入百度地圖SDK與創(chuàng)建應(yīng)用就不多說(shuō)了,百度的文檔應(yīng)該比我說(shuō)的更詳細(xì),下面直接正文吧

1.首先地圖的初始化

- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

}

/// 地圖

- (void)setupView {

_mapView = [[BMKMapView alloc]init];

[_mapView setMapType:BMKMapTypeStandard];// 地圖類型 ->衛(wèi)星/標(biāo)準(zhǔn)、

_mapView.showsUserLocation = YES;

_mapView.gesturesEnabled = YES;

_mapView.delegate = self; // 此處記得不用的時(shí)候需要置nil,否則影響內(nèi)存的釋放

_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;//先關(guān)閉顯示的定位圖層

_mapView.userTrackingMode = BMKUserTrackingModeFollow;//設(shè)置定位的狀態(tài)

_mapView.showsUserLocation = YES;//顯示定位圖層

_clusterManager = [[BMKClusterManager alloc] init];

//初始化檢索對(duì)象

self.districtSearch = [[BMKDistrictSearch alloc] init];

//設(shè)置delegate,用于接收檢索結(jié)果

self.districtSearch.delegate = self;

//在此處理正常結(jié)果

_clusterCaches = [[NSMutableArray alloc] init];

for (NSInteger i = 3; i < 22; i++) {

[_clusterCaches addObject:[NSMutableArray array]];

? ? ?}

}


2.地圖的開(kāi)始與加載完畢

/**

*地圖初始化完畢時(shí)會(huì)調(diào)用此接口

*@param mapView 地圖View

*/

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {

BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];

displayParam.isAccuracyCircleShow = NO;//精度圈是否顯示

[_mapView updateLocationViewWithParam:displayParam];

BMKCoordinateRegion region ;//表示范圍的結(jié)構(gòu)體

region.center = _mapView.centerCoordinate;//中心點(diǎn)

self.cCoordinate = _mapView.centerCoordinate;//中心點(diǎn)

region.span.latitudeDelta = 0.002;//經(jīng)度范圍(設(shè)置為0.1表示顯示范圍為0.2的緯度范圍)

region.span.longitudeDelta = 0.002;//緯度范圍

[_mapView setRegion:region animated:YES];

[self updateClusters];

}

/**

*地圖渲染每一幀畫面過(guò)程中,以及每次需要重繪地圖時(shí)(例如添加覆蓋物)都會(huì)調(diào)用此接口

*@param mapView 地圖View

*@param status 此時(shí)地圖的狀態(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 {

//屏幕坐標(biāo)轉(zhuǎn)地圖經(jīng)緯度

CLLocationCoordinate2D MapCoordinate = [_mapView convertPoint:_mapView.center toCoordinateFromView:_mapView];

if (_reverseGeoCodeOption==nil) {

//初始化反地理編碼類

_reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];

}

//需要逆地理編碼的坐標(biāo)位置

_reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;

[_geoCodeSerch reverseGeoCode:_reverseGeoCodeOption];

// 如果你是請(qǐng)求自己后臺(tái)的數(shù)據(jù)可以在這里請(qǐng)求,可以省去下面檢索回來(lái)的數(shù)據(jù),由于我的是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ā)送失敗");

? ? }

}




// 當(dāng)?shù)貓D發(fā)生改變之后,檢索并

//實(shí)現(xiàn)PoiSearchDeleage處理回調(diào)結(jié)果

- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error

{

if (error == BMK_SEARCH_NO_ERROR) {

//在此處理正常結(jié)果

}

else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){

//當(dāng)在設(shè)置城市未找到結(jié)果,但在其他城市找到結(jié)果時(shí),回調(diào)建議檢索城市列表

// result.cityList;

NSLog(@"起始點(diǎn)有歧義");

} else {

NSLog(@"抱歉,未找到結(jié)果");

}

// 清空緩存數(shù)據(jù)

?[_clusterManager clearClusterItems];

?for (BMKPoiInfo *poiInfo in poiResultList.poiInfoList) {

? XJCluster *cluster = [[XJCluster alloc] init];

? cluster.name = poiInfo.name;

? ?cluster.pt = poiInfo.pt;

? ? // 添加數(shù)據(jù)

? ?[self addAnnoWithPT:cluster];

? ?}

}


4.更新點(diǎn)聚合狀態(tài)

- (void)updateClusters {

_clusterZoom = (NSInteger)_mapView.zoomLevel;

@synchronized(_clusterCaches) {

NSMutableArray *clusters = [NSMutableArray array];

dispatch_async(dispatch_get_global_queue(0, 0), ^{

///獲取聚合后的標(biāo)注

__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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容