如何从MKMapView的可见区域获取半径?

6
我能够获取地图视图的可见矩形,以及地图视图的中心点和跨度差也可以从mkmap视图方法中获取: 要获取可见区域:mapView.visibleMapRect被使用。要获取中心点: map view.centerCoordinate 被使用,并且要获取跨度: mapView.region.span 被使用。
现在我有了所有信息,如何使用计算方式计算出可见区域的半径呢?有人可以详细解释一下吗?
我看过这个问题,但是答案给出的是跨度而不是可见区域的半径。
2个回答

10

要获得半径,请按照以下步骤进行:

- (CLLocationDistance)getRadius
{
    CLLocationCoordinate2D centerCoor = [self getCenterCoordinate];
    // init center location from center coordinate
    CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:centerCoor.latitude longitude:centerCoor.longitude];

    CLLocationCoordinate2D topCenterCoor = [self getTopCenterCoordinate];
    CLLocation *topCenterLocation = [[CLLocation alloc] initWithLatitude:topCenterCoor.latitude longitude:topCenterCoor.longitude];

    CLLocationDistance radius = [centerLocation distanceFromLocation:topCenterLocation];

    return radius;
}

它将返回以为单位的半径。

要获取中心坐标

- (CLLocationCoordinate2D)getCenterCoordinate
{
    return [self.mapView centerCoordinate];
}

要获取半径,取决于您想获取第二个点的位置。让我们选择顶部中心

- (CLLocationCoordinate2D)getTopCenterCoordinate
{
    // to get coordinate from CGPoint of your map
    return [self.mapView convertPoint:CGPointMake(self.mapView.frame.size.width / 2.0f, 0) toCoordinateFromView:self.mapView];
}

1
对于所有 Swift 爱好者:这是一个 MKMapView 的简短扩展(在此我使用左上角作为半径):extension MKMapView{ var topLeftCoordinate: CLLocationCoordinate2D{ return convert(CGPoint.zero, toCoordinateFrom: self) } var radius: CLLocationDistance{ let centerLocation = CLLocation(latitude: centerCoordinate.latitude, longitude: centerCoordinate.longitude) let topLeftLocation = CLLocation(latitude: topLeftCoordinate.latitude, longitude: topLeftCoordinate.longitude) return centerLocation.distance(from: topLeftLocation) } } - Finn Fahrenkrug

10

使用 Swift 3.0,您可以使用扩展来简化您的生活:

extension MKMapView {

    func topCenterCoordinate() -> CLLocationCoordinate2D {
        return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
    }

    func currentRadius() -> Double {
        let centerLocation = CLLocation(coordinate: self.centerCoordinate)
        let topCenterCoordinate = self.topCenterCoordinate()
        let topCenterLocation = CLLocation(coordinate: topCenterCoordinate)
        return centerLocation.distance(from: topCenterLocation)
    }

}

使用 Swift 4.0

extension MKMapView {

    func topCenterCoordinate() -> CLLocationCoordinate2D {
        return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
    }

    func currentRadius() -> Double {
        let centerLocation = CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)
        let topCenterCoordinate = self.topCenterCoordinate()
        let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
        return centerLocation.distance(from: topCenterLocation)
    }

}

2
Swift 4: 将 CLLocation(coordinate: *) 更改为 CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude) - Michael
Finn的Swift翻译效果更好。 - BuguiBu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接