15

当我需要制作时MKCoordinateRegion,我会执行以下操作:

var region = MKCoordinateRegion
               .FromDistance(coordinate, RegionSizeInMeters, RegionSizeInMeters);

非常简单 - 完美运行。

现在我希望存储当前区域span的值。当我查看该region.Span值时,它MKCoordinateSpan具有两个属性:

public double LatitudeDelta;
public double LongitudeDelta;

如何将LatitudeDelta值转换为latitudinalMeters请?(所以我可以使用上述方法重新创建我的区域(稍后)......

4

3 回答 3

37

正如我所看到的,您已经有了地图的区域。它不仅包含经纬度三角洲,还包含该区域的中心点。您可以计算以米为单位的距离,如图所示:

在此处输入图像描述

1:获取区域跨度(区域有多大,以纬度/经度为单位)

MKCoordinateSpan span = region.span;

2:获取区域中心(纬度/经度坐标)

CLLocationCoordinate2D center = region.center;

3:根据中心位置创建两个位置(loc1 & loc2,北 - 南)并计算其间的距离(以米为单位)

//get latitude in meters
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:(center.latitude - span.latitudeDelta * 0.5) longitude:center.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:(center.latitude + span.latitudeDelta * 0.5) longitude:center.longitude];
int metersLatitude = [loc1 distanceFromLocation:loc2];

4:根据中心位置创建两个位置(loc3 & loc4, west - east)并计算其间的距离(以米为单位)

//get longitude in meters
CLLocation *loc3 = [[CLLocation alloc] initWithLatitude:center.latitude longitude:(center.longitude - span.longitudeDelta * 0.5)];
CLLocation *loc4 = [[CLLocation alloc] initWithLatitude:center.latitude longitude:(center.longitude + span.longitudeDelta * 0.5)];
int metersLongitude = [loc3 distanceFromLocation:loc4];
于 2014-01-22T06:10:41.817 回答
15

Hanne 解决方案的 Swift 实现:

    let span = mapView.region.span
    let center = mapView.region.center
    
    let loc1 = CLLocation(latitude: center.latitude - span.latitudeDelta * 0.5, longitude: center.longitude)
    let loc2 = CLLocation(latitude: center.latitude + span.latitudeDelta * 0.5, longitude: center.longitude)
    let loc3 = CLLocation(latitude: center.latitude, longitude: center.longitude - span.longitudeDelta * 0.5)
    let loc4 = CLLocation(latitude: center.latitude, longitude: center.longitude + span.longitudeDelta * 0.5)
    
    let metersInLatitude = loc1.distanceFromLocation(loc2)
    let metersInLongitude = loc3.distanceFromLocation(loc4)

编辑:

For Swift 5distanceFromLocation(_:)已重命名为distance(from:)意味着最后两行现在改为

   let metersInLatitude = loc1.distance(from: loc2)
   let metersInLongitude = loc3.distance(from: loc4)
于 2016-01-11T14:45:56.033 回答
2

基于上述,这是我在地图项目中使用的一个有用的扩展:

extension MKCoordinateRegion {
    public var radius: CLLocationDistance {
        let loc1 = CLLocation(latitude: center.latitude - span.latitudeDelta * 0.5, longitude: center.longitude)
        let loc2 = CLLocation(latitude: center.latitude + span.latitudeDelta * 0.5, longitude: center.longitude)
        let loc3 = CLLocation(latitude: center.latitude, longitude: center.longitude - span.longitudeDelta * 0.5)
        let loc4 = CLLocation(latitude: center.latitude, longitude: center.longitude + span.longitudeDelta * 0.5)
    
        let metersInLatitude = loc1.distance(from: loc2)
        let metersInLongitude = loc3.distance(from: loc4)
        let radius = max(metersInLatitude, metersInLongitude) / 2.0
        return radius
    }
}
于 2021-09-10T00:25:01.883 回答