IOS dynamically displays the data in a visible range based on the coordinate region of the coordinate data point.

Source: Internet
Author: User

After marking many points on a map, you can set the center point of the map, but the scaling level is a bit difficult to use,

Therefore, it is necessary to dynamically calculate based on the coordinate region of the coordinate data point, and display all the points in the visible range of the map.

Directly run the Code:

 

// Clear the views and data of coordinate data [_ bMapView removeAnnotations: _ mapAnnotations]; [_ mapAnnotations removeAllObjects]; [_ carPointArray removeAllObjects]; // declare the filtering of the location and region of the coordinate data during resolution, including the minimum and maximum CLLocationDegrees minLat of longitude and latitude; CLLocationDegrees maxLat; CLLocationDegrees minLon; CLLocationDegrees maxLon; // parse the data for (int I = 0; I <rows. count; I ++) {NSDictionary * row = [rows objectAtIndex: I]; coordinate model class * item = [[Coordinate model class alloc] initWithJson: row]; if (item. vehicleNo & [item. vehicleNo length]> 0) {annotation model class * annotation = [[annotation model class alloc] init]; annotation. coordinate = item. baiduCoordinate; annotation. item = item; [_ mapAnnotations addObject: annotation]; [_ bMapView addAnnotation: annotation]; [annotation release]; if (I = 0) {// use the first coordinate point as the initial value minLat = item. baiduCoordinate. latitude; maxLat = item. baiduCoordinate. latitude; minLon = item. baiduCoordinate. longpolling; maxLon = item. baiduCoordinate. longpolling;} else {// compare and filter out the minimum latitude and maximum latitude. The minimum longitude and the maximum longitude minLat = MIN (minLat, item. baiduCoordinate. latitude); maxLat = MAX (maxLat, item. baiduCoordinate. latitude); minLon = MIN (minLon, item. baiduCoordinate. longpolling); maxLon = MAX (maxLon, item. baiduCoordinate. longpolling);} [_ carPointArray addObject: item];} [item release];} // dynamic region based on coordinate data, to determine the display center and zoom level of the map if (_ carPointArray. count> 0) {// calculate the center point CLLocationCoordinate2D centCoor; centCoor. latitude = (CLLocationDegrees) (maxLat + minLat) * 0.5f); centCoor. longpolling = (CLLocationDegrees) (maxLon + minLon) * 0.5f); BMKCoordinateSpan span; // calculates the span of a geographical location. latitudeDelta = maxLat-minLat; span. longitudeDelta = maxLon-minLon; // obtain the data coordinate region BMKCoordinateRegion region = BMKCoordinateRegionMake (centCoor, span ); // The coordinate range of Baidu map is converted to the position of the relative view CGRect fitRect = [_ bMapView convertRegion: region toRectToView: _ bMapView]; // convert the location of the map view to the location BMKMapRect fitMapRect = [_ bMapView convertRect: fitRect toMapRectFromView: _ bMapView]; // set the visible range of the map to the location of the map where the data is located [_ bMapView setVisibleMapRect: fitMapRect animated: YES];}

  

Supplement:

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

  

 

 

Finally, let's see:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.