## 如何创建一个给定两个点的MKMapRect，每个点指定一个纬度和经度值？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (37)

MKMapRect MKMapRectMake(
double x,
double y,
double width,
double height
);

Parameters
x
The point along the east-west axis of the map projection to use for the origin.
y
The point along the north-south axis of the map projection to use for the origin.
width
The width of the rectangle (measured using map points).
height
The height of the rectangle (measured using map points).
Return Value
A map rectangle with the specified values.

24.7433195, -124.7844079
49.3457868, -66.9513812

### 2 个回答

// these are your two lat/long coordinates
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(lat1,long1);
CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake(lat2,long2);

// convert them to MKMapPoint
MKMapPoint p1 = MKMapPointForCoordinate (coordinate1);
MKMapPoint p2 = MKMapPointForCoordinate (coordinate2);

// and make a MKMapRect using mins and spans
MKMapRect mapRect = MKMapRectMake(fmin(p1.x,p2.x), fmin(p1.y,p2.y), fabs(p1.x-p2.x), fabs(p1.y-p2.y));

let coordinates: [CLLocationCoordinate2D] = …
let points = coordinates.map { MKMapPointForCoordinate(\$0) }
let rects = points.map { MKMapRect(origin: \$0, size: MKMapSize(width: 0, height: 0)) }
let fittingRect = rects.reduce(MKMapRectNull, combine: MKMapRectUnion)