在地圖上標(biāo)注很多點(diǎn)之后,地圖的中心點(diǎn)可以設(shè)置,但是縮放級(jí)別用起來(lái)就有點(diǎn)囧了,
所以,就需要根據(jù)坐標(biāo)數(shù)據(jù)點(diǎn)所在的坐標(biāo)區(qū)域來(lái)動(dòng)態(tài)計(jì)算,把所有點(diǎn)都剛好顯示到地圖的可視范圍內(nèi)。
直接上代碼:
//清理坐標(biāo)數(shù)據(jù)的視圖和數(shù)據(jù) [_bMapView removeAnnotations:_mapAnnotations]; [_mapAnnotations removeAllObjects]; [_carPointArray removeAllObjects]; //聲明解析時(shí)對(duì)坐標(biāo)數(shù)據(jù)的位置區(qū)域的篩選,包括經(jīng)度和緯度的最小值和最大值 CLLocationDegrees minLat; CLLocationDegrees maxLat; CLLocationDegrees minLon; CLLocationDegrees maxLon; //解析數(shù)據(jù) for (int i=0; i<rows.count; i++) { NSDictionary *row = [rows objectAtIndex:i]; 坐標(biāo)模型類(lèi) *item = [[坐標(biāo)模型類(lèi) alloc] initWithJson:row]; if (item.vehicleNo && [item.vehicleNo length]>0) { 標(biāo)注模型類(lèi) *annotation = [[標(biāo)注模型類(lèi) alloc] init]; annotation.coordinate = item.baiduCoordinate; annotation.item = item; [_mapAnnotations addObject:annotation]; [_bMapView addAnnotation:annotation]; [annotation release]; if (i==0) { //以第一個(gè)坐標(biāo)點(diǎn)做初始值 minLat = item.baiduCoordinate.latitude; maxLat = item.baiduCoordinate.latitude; minLon = item.baiduCoordinate.longitude; maxLon = item.baiduCoordinate.longitude; }else{ //對(duì)比篩選出最小緯度,最大緯度;最小經(jīng)度,最大經(jīng)度 minLat = MIN(minLat, item.baiduCoordinate.latitude); maxLat = MAX(maxLat, item.baiduCoordinate.latitude); minLon = MIN(minLon, item.baiduCoordinate.longitude); maxLon = MAX(maxLon, item.baiduCoordinate.longitude); } [_carPointArray addObject:item]; } [item release]; } //動(dòng)態(tài)的根據(jù)坐標(biāo)數(shù)據(jù)的區(qū)域,來(lái)確定地圖的顯示中心點(diǎn)和縮放級(jí)別 if (_carPointArray.count > 0) { //計(jì)算中心點(diǎn) CLLocationCoordinate2D centCoor; centCoor.latitude = (CLLocationDegrees)((maxLat+minLat) * 0.5f); centCoor.longitude = (CLLocationDegrees)((maxLon+minLon) * 0.5f); BMKCoordinateSpan span; //計(jì)算地理位置的跨度 span.latitudeDelta = maxLat - minLat; span.longitudeDelta = maxLon - minLon; //得出數(shù)據(jù)的坐標(biāo)區(qū)域 BMKCoordinateRegion region = BMKCoordinateRegionMake(centCoor, span); //百度地圖的坐標(biāo)范圍轉(zhuǎn)換成相對(duì)視圖的位置 CGRect fitRect = [_bMapView convertRegion:region toRectToView:_bMapView]; //將地圖視圖的位置轉(zhuǎn)換成地圖的位置 BMKMaPRect fitMapRect = [_bMapView convertRect:fitRect toMapRectFromView:_bMapView]; //設(shè)置地圖可視范圍為數(shù)據(jù)所在的地圖位置 [_bMapView setVisibleMapRect:fitMapRect animated:YES]; }
補(bǔ)充:
MKMapRect zoomRect = MKMapRectNull;for (id <MKAnnotation> annotation in mapView.annotations) { MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); if (MKMapRectIsNull(zoomRect)) { zoomRect = pointRect; } else { zoomRect = MKMapRectUnion(zoomRect, pointRect); }}[mapView setVisibleMapRect:zoomRect animated:YES];
最后來(lái)張效果圖:

新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注