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: