如何创建一个给定两个点的MKMapRect,每个点都用纬度和经度值指定?

Joh*_*rck 27 mapkit ios

我有一个扩展NSObject和实现MKOverlay协议的自定义类.因此,我需要实现协议的boundingMapRect属性,这是一个MKMapRect.创造一个MKMapRect我当然可以MKMapRectMake用来做一个.但是,我不知道如何MKMapRect使用我拥有的数据来创建两个点,每个点由纬度和经度指定.MKMapRectMake的文档说:

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.
Run Code Online (Sandbox Code Playgroud)

我必须指出的纬度和经度值MKMapRect是:

24.7433195, -124.7844079
49.3457868, -66.9513812
Run Code Online (Sandbox Code Playgroud)

MKMapRect因此,目标需要指出一个看起来像这样的区域:目标MKMapRect

那么,重申一下,如何使用我的lat/lon值创建一个MKMapRect我可以设置为MKOverlay协议@property (nonatomic, readonly) MKMapRect boundingMapRect属性的值?

CSm*_*ith 40

这应该这样做:

// 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));
Run Code Online (Sandbox Code Playgroud)

这将使用两个x和y坐标中较小的一个作为起点,并计算宽度和高度两点之间的x/y跨度.

  • 这在第 180 条子午线附近不起作用。验证来源:CLLocationCoordinate2D(纬度:-33.8392932,经度:151.21519799999999)目的地:CLLocationCoordinate2D(纬度:39.645516999999998,经度:-1704)5。 (3认同)

Pat*_*pel 23

对于任意数量的坐标,在Swift(4.2)中:

// Assuming `coordinates` is of type `[CLLocationCoordinate2D]`
let rects = coordinates.lazy.map { MKMapRect(origin: MKMapPoint($0), size: MKMapSize()) }
let fittingRect = rects.reduce(MKMapRect.null) { $0.union($1) }
Run Code Online (Sandbox Code Playgroud)

正如@Abin Baby所指出的那样,这不会考虑周围环境(在+/- 180经度和+/- 90纬度).结果仍然是正确的,但它不是最小的矩形.


bol*_*iva 6

基于帕特里克的答案延伸MKMapRect:

extension MKMapRect {
    init(coordinates: [CLLocationCoordinate2D]) {
        self = coordinates.map({ MKMapPointForCoordinate($0) }).map({ MKMapRect(origin: $0, size: MKMapSize(width: 0, height: 0)) }).reduce(MKMapRectNull, combine: MKMapRectUnion)
    }
}
Run Code Online (Sandbox Code Playgroud)


vau*_*all 6

这对我有用。

即使跨越 +/-180 经度和 +/-90 纬度之间也没有问题。

斯威夫特4.2

func makeRect(coordinates:[CLLocationCoordinate2D]) -> MKMapRect {
    var rect = MKMapRect()
    var coordinates = coordinates
    if !coordinates.isEmpty {
        let first = coordinates.removeFirst()
        var top = first.latitude
        var bottom = first.latitude
        var left = first.longitude
        var right = first.longitude
        coordinates.forEach { coordinate in
            top = max(top, coordinate.latitude)
            bottom = min(bottom, coordinate.latitude)
            left = min(left, coordinate.longitude)
            right = max(right, coordinate.longitude)
        }
        let topLeft = MKMapPoint(CLLocationCoordinate2D(latitude:top, longitude:left))
        let bottomRight = MKMapPoint(CLLocationCoordinate2D(latitude:bottom, longitude:right))
        rect = MKMapRect(x:topLeft.x, y:topLeft.y,
                         width:bottomRight.x - topLeft.x, height:bottomRight.y - topLeft.y)
    }
    return rect
}
Run Code Online (Sandbox Code Playgroud)