首先說下因?yàn)槲覐氖碌腁ndroid開發(fā),百度地圖jforAndroid在項(xiàng)目中接觸過,沒有細(xì)細(xì)研究過,一直用的高德,對于添加多邊形覆蓋物這個(gè)功能,我用的還是比較多的,對于Android版的高德地圖,在API中已經(jīng)完美集成了這個(gè)功能了,但是百度forIOS,用起來就沒有那么方便了,只有靜態(tài)的添加方法,沒辦法動(dòng)態(tài)添加,得自己費(fèi)一番功夫,國際慣例,先上動(dòng)圖,也許我的實(shí)現(xiàn)不是最好的,歡迎一起學(xué)習(xí)交流!

aa.gif
要實(shí)現(xiàn)這個(gè)效果,最先要解決的就是地圖的屏幕點(diǎn)擊事件,看了API,發(fā)現(xiàn)百度屏幕點(diǎn)擊事件有三個(gè)方法:
-(void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi{}//點(diǎn)擊地圖POI的時(shí)候觸發(fā)
-(void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{}//點(diǎn)擊地圖空白區(qū)域的時(shí)候觸發(fā)
-(void)mapView:(BMKMapView *)mapView onClickedBMKOverlayView:(BMKOverlayView *)overlayView{}//點(diǎn)擊地圖Overlay的時(shí)候觸發(fā)
看了上面三個(gè)方法有點(diǎn)小崩潰,我們需要在onClickedMapPoi()和onClickedMapBlank()中分別實(shí)現(xiàn)我們的方法,這里不知道有沒有更好方法,因?yàn)檫@么實(shí)現(xiàn)的話會(huì)有點(diǎn)小問題。
接著就是實(shí)現(xiàn)添加地理圍欄的方法了,奉上官方的代碼:
- (void)viewDidLoad {
[super viewDidLoad];
// 添加多邊形覆蓋物
CLLocationCoordinate2D coords[3] = {0};
coords[0].latitude = 39;
coords[0].longitude = 116;
coords[1].latitude = 38;
coords[1].longitude = 115;
coords[2].latitude = 38;
coords[2].longitude = 117;
BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:3];
[_mapView addOverlay:polygon];
}
// Override
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
if ([overlay isKindOfClass:[BMKPolygon class]]){
BMKPolygonView* polygonView = [[[BMKPolygonView alloc] initWithOverlay:overlay] autorelease];
polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
polygonView.lineWidth = 5.0;
return polygonView;
}
return nil;
}

2182D407-40A7-43DA-BEBF-1992B5875E74.png
這里最重要的是CLLocationCoordinate2D,只要能動(dòng)態(tài)添加這個(gè)數(shù)組內(nèi)的元素,那么一切就迎刃而解了。好的,來了,動(dòng)態(tài)分配數(shù)組:
CLLocationCoordinate2D * coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D)*size);
好吧,直接上代碼:
-(void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi{
CLLocationCoordinate2D coordinate = mapPoi.pt;//獲取點(diǎn)擊坐標(biāo)
[self addPointAnnotation:coordinate];
if (self.array == nil) {
self.array = [[NSMutableArray alloc] init];//初始化數(shù)組,用來存儲(chǔ)坐標(biāo)
}
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[self.array addObject:location];//坐標(biāo)轉(zhuǎn)換用,因?yàn)镹SMutableArray無法直接存儲(chǔ) CLLocationCoordinate2D
NSInteger size = [self.array count];
//大于三的時(shí)候才添加
if (size >= 3) {
NSArray *a = [self.map overlays];
[self.map removeOverlays:a];
//動(dòng)態(tài)分配數(shù)組
CLLocationCoordinate2D * coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D)*size);//size就是數(shù)組的大小
for (int i = 0; i < [self.array count]; i++) {
CLLocation *location = [self.array objectAtIndex:i];
coords[i].latitude = location.coordinate.latitude;
coords[i].longitude = location.coordinate.longitude;
}
BMKPolygon *polygon2 = [BMKPolygon polygonWithCoordinates:coords count:size];
[self.map addOverlay:polygon2];
}
[location release];
}
以上!