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

时间:2014-06-03 20:13:54

标签: ios mapkit

我有一个扩展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. 的文档声明:

MKMapRect

我必须指出24.7433195, -124.7844079 49.3457868, -66.9513812 的纬度和经度值是:

MKMapRect

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

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

4 个答案:

答案 0 :(得分:38)

这应该这样做:

// 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));

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

答案 1 :(得分: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) }

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

答案 2 :(得分:6)

根据Patrick在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)
    }
}

答案 3 :(得分:0)

这对我有用。

即使在+/- 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
}