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

时间:2014-01-22 02:51:47

标签: ios mkmapview mapkit mkcoordinateregion mkcoordinatespan

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

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

非常简单 - 效果很好。

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

public double LatitudeDelta;
public double LongitudeDelta;

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

2 个答案:

答案 0 :(得分:29)

我可以看到你已经拥有了地图的区域。它不仅包含lat&长三角洲,但也是该地区的中心点。您可以计算距离(以米为单位),如图所示:

enter image description here

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

答案 1 :(得分:10)

Hannes解决方案的快速实施:

    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)