iOS根據座標資料點所在的座標地區來動態顯示到可視範圍

來源:互聯網
上載者:User

在地圖上標註很多點之後,地圖的中心點可以設定,但是縮放層級用起來就有點囧了,

所以,就需要根據座標資料點所在的座標地區來動態計算,把所有點都剛好顯示到地圖的可視範圍內。

直接上代碼:

 

    //清理座標資料的視圖和資料    [_bMapView removeAnnotations:_mapAnnotations];    [_mapAnnotations removeAllObjects];    [_carPointArray removeAllObjects];    //聲明解析時對座標資料的位置地區的篩選,包括經度和緯度的最小值和最大值    CLLocationDegrees minLat;    CLLocationDegrees maxLat;    CLLocationDegrees minLon;    CLLocationDegrees maxLon;    //解析資料    for (int i=0; i<rows.count; i++) {        NSDictionary *row = [rows objectAtIndex:i];        座標模型類 *item = [[座標模型類 alloc] initWithJson:row];        if (item.vehicleNo && [item.vehicleNo length]>0) {            標註模型類 *annotation = [[標註模型類 alloc] init];            annotation.coordinate = item.baiduCoordinate;            annotation.item = item;            [_mapAnnotations addObject:annotation];            [_bMapView addAnnotation:annotation];            [annotation release];                        if (i==0) {                //以第一個座標點做初始值                minLat = item.baiduCoordinate.latitude;                maxLat = item.baiduCoordinate.latitude;                minLon = item.baiduCoordinate.longitude;                maxLon = item.baiduCoordinate.longitude;            }else{                //對比篩選出最小緯度,最大緯度;最小經度,最大經度                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];    }    //動態根據座標資料的地區,來確定地圖的顯示中心點和縮放層級    if (_carPointArray.count > 0) {        //計算中心點        CLLocationCoordinate2D centCoor;        centCoor.latitude = (CLLocationDegrees)((maxLat+minLat) * 0.5f);        centCoor.longitude = (CLLocationDegrees)((maxLon+minLon) * 0.5f);        BMKCoordinateSpan span;        //計算地理位置的跨度        span.latitudeDelta = maxLat - minLat;        span.longitudeDelta = maxLon - minLon;        //得出資料的座標地區        BMKCoordinateRegion region = BMKCoordinateRegionMake(centCoor, span);        //百度地圖的座標範圍轉換成相對視圖的位置        CGRect fitRect = [_bMapView convertRegion:region toRectToView:_bMapView];        //將地圖視圖的位置轉換成地圖的位置        BMKMapRect fitMapRect = [_bMapView convertRect:fitRect toMapRectFromView:_bMapView];        //設定地圖可視範圍為資料所在的地圖位置        [_bMapView setVisibleMapRect:fitMapRect animated:YES];            }

  

補充:

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];

  

 

 

最後來張:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.