尝试获取iOS MKCoordinateSpan的跨度大小(以米为单位)

问题描述 投票:11回答:2

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

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

很简单 - 效果很好。

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

public double LatitudeDelta;
public double LongitudeDelta;

我怎样才能将LatitudeDelta值转换为latitudinalMeters? (那么我可以使用上面的方法重新创建我的区域(稍后)...

ios mkmapview mapkit mkcoordinateregion mkcoordinatespan
2个回答
29
投票

我可以看到你已经有了地图的区域。它不仅包含纬度和长度增量,还包含该区域的中心点。您可以计算距离(以米为单位),如图所示:

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,西 - 东)并计算两者之间的距离(以米为单位)

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

10
投票

Hannes解决方案的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)
© www.soinside.com 2019 - 2024. All rights reserved.