After a lot of points are marked on the map, the center point of the map can be set, but the zoom level is a bit embarrassing.
Therefore, it is necessary to dynamically calculate the coordinates of the coordinate data points, and all the points are displayed exactly within the visual range of the map.
Directly on the code:
Clean up view and data for coordinate data [_bmapview removeannotations:_mapannotations]; [_mapannotations removeallobjects]; [_carpointarray removeallobjects]; The filter for the location area of the coordinate data when the declaration is resolved, including the minimum and maximum values of longitude and latitude cllocationdegrees Minlat; Cllocationdegrees Maxlat; Cllocationdegrees Minlon; Cllocationdegrees Maxlon; Parse 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) {Callout model class *annotation = [Callout model class alloc] init]; Annotation.coordinate = item.baiducoordinate; Annotation.item = Item; [_mapannotations addobject:annotation]; [_bmapview addannotation:annotation]; [Annotation release]; if (i==0) {//with the first sitting punctuation to do the initial value Minlat = Item.baiduCoordinate.latitude; Maxlat = Item.baiduCoordinate.latitude; Minlon = item.baiduCoordinate.longitude; Maxlon = Item.baiduCoordinate.longitude; }else{//Contrast filters out minimum latitude, maximum latitude, minimum longitude, maximum longitude 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]; }//Dynamically determine the display center point and zoom level of the map based on the area of the coordinate data if (_carpointarray.count > 0) {//center point cllocationcoordinate2d CE Ntcoor; Centcoor.latitude = (cllocationdegrees) ((maxlat+minlat) * 0.5f); Centcoor.longitude = (cllocationdegrees) ((maxlon+minlon) * 0.5f); Bmkcoordinatespan span; Calculates the span of a geographic location span.latitudedelta = Maxlat-minlat; Span.longitudedelta = Maxlon-minlon; The coordinate area of the data is bmkcoordinateregion region = Bmkcoordinateregionmake (Centcoor, span); The coordinate range of the Baidu map is converted to the relative view of the location cgrect fitrect = [_bmapview convertregion:region Torecttoview:_bmapview]; Converts the location of the map view to the location of the map bmkmaprect fitmaprect = [_bmapview convertrect:fitrect Tomaprectfromview:_bmapview]; Set the map visual range to the map location where the data resides [_bmapview Setvisiblemaprect:fitmaprect Animated:yes]; }
Add:
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, come to Zhang: