国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

iOS根據(jù)坐標(biāo)數(shù)據(jù)點(diǎn)所在的坐標(biāo)區(qū)域來(lái)動(dòng)態(tài)顯示到可視范圍

2019-11-14 20:22:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在地圖上標(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)張效果圖:

 

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 襄城县| 平南县| 平顶山市| 兰坪| 杂多县| 平武县| 合阳县| 广水市| 定远县| 涡阳县| 祁东县| 潢川县| 四川省| 兴文县| 禄丰县| 北碚区| 洛宁县| 大宁县| 贵阳市| 呼和浩特市| 鲁甸县| 临邑县| 屏东市| 香格里拉县| 兴隆县| 当雄县| 五家渠市| 香格里拉县| 黄骅市| 苗栗市| 临朐县| 光山县| 锦州市| 文水县| 林芝县| 林口县| 镇康县| 长寿区| 叙永县| 开江县| 英山县|