如何创建给出了两个百分点MKMapRect,每一个经度和纬度值规定?

问题描述 投票:27回答:4

我有一个扩展NSObject和实现MKOverlay协议的自定义类。其结果是,我需要实现该协议的boundingMapRect属性,它是一个MKMapRect。要创建一个MKMapRect我当然可以用MKMapRectMake做一个。但是,我不知道如何使用这些数据我有这两点,每一个经度和纬度指定创建MKMapRectMKMapRectMake的文档状态:

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协议的MKOverlay财产@property (nonatomic, readonly) MKMapRect boundingMapRect

ios mapkit
4个回答
39
投票

这应该这样做:

// 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两个点的宽度和高度之间的跨越。


23
投票

对于任何数量的坐标,在斯威夫特(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婴儿所指出的,这将不采取环绕考虑(在+/- 180经度&+/- 90纬度)。结果仍然是正确的,但它不会是最小可能的矩形。


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)
    }
}

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
}
© www.soinside.com 2019 - 2024. All rights reserved.