Trying to get the span size in meters for an iOS MKCoordinateSpan

大兔子大兔子 提交于 2019-12-03 07:32:32

问题


When I need to make an MKCoordinateRegion, I do the following:

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

very simple - works perfectly.

Now I wish to store the value of the current region span. When i look at the region.Span value, it’s an MKCoordinateSpan which has two properties:

public double LatitudeDelta;
public double LongitudeDelta;

How can I convert the LatitudeDelta value into a latitudinalMeters please? (So then I can recreate my region (later on) using the above method...


回答1:


As I can see you already have the region of the map. It doesn't only contain the lat & long deltas but also the center point of the region. You can calculate the distances in meters as illustrated in the picture:

1: Get the region span (how big the region is in lat/long degrees)

MKCoordinateSpan span = region.span;

2: Get the region center (lat/long coordinates)

CLLocationCoordinate2D center = region.center;

3: Create two locations (loc1 & loc2, north - south) based on the center location and calculate the distance inbetween (in meters)

//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: Create two locations (loc3 & loc4, west - east) based on the center location and calculate the distance inbetween (in meters)

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



回答2:


Swift implementation for Hannes solution:

    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)


来源:https://stackoverflow.com/questions/21273269/trying-to-get-the-span-size-in-meters-for-an-ios-mkcoordinatespan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!