其他技术文档提供的也是可以的,不过存在一个边点问题,也就是说一个点刚好在屏幕的边缘,不符合需求 ,话不多说,直接上代码
- (BMKCoordinateRegion)getCoordinateRegionWithPoints:(NSArray <CLLocation *>*)points{
double minLat = 90.0;
double maxLat = -90.0;
double minLon = 180.0;
double maxLon = -180.0;
for (size_t i = 0; i < points.count; i++) {
minLat = fmin(minLat, ((CLLocation *)points[i]).coordinate.latitude );
maxLat = fmax(maxLat,((CLLocation *)points[i]).coordinate.latitude);
minLon = fmin(minLon, ((CLLocation *)points[i]).coordinate.longitude);
maxLon = fmax(maxLon, ((CLLocation *)points[i]).coordinate.longitude);
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat) * 0.5, (minLon + maxLon) * 0.5);
CLLocationDegrees latDiff = (center.latitude - minLat) * 2.0;
CLLocationDegrees longDiff = (center.longitude - minLon) * 2.0;
BMKCoordinateSpan span = {fabs(latDiff)+0.05,fabs(longDiff)+0.05};
BMKCoordinateRegion region = {center, span};
return region;
}
最主要的设置
BMKCoordinateSpan span = {fabs(latDiff)+0.05,fabs(longDiff)+0.05};
0.05 是你需要调整的
效果如下
喜欢的话,点个关注