首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图获得iOS MKCoordinateSpan的跨度(以米为单位)

试图获得iOS MKCoordinateSpan的跨度(以米为单位)
EN

Stack Overflow用户
提问于 2014-01-22 02:51:47
回答 3查看 13.8K关注 0票数 15

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

代码语言:javascript
复制
var region = MKCoordinateRegion
               .FromDistance(coordinate, RegionSizeInMeters, RegionSizeInMeters);

很简单-效果很好。

现在我想存储当前区域跨度的值。当我查看region.Span值时,它是一个具有两个属性的MKCoordinateSpan

代码语言:javascript
复制
public double LatitudeDelta;
public double LongitudeDelta;

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-22 06:10:41

如我所见,你已经有了地图的区域。它不仅包含拉特-长三角,而且是该区域的中心点。如图所示,您可以计算出以米为单位的距离:

1:获得区域跨度(该区域在拉特角/长度上有多大)

MKCoordinateSpan span = region.span;

2:获取区域中心(lat/长坐标)

CLLocationCoordinate2D center = region.center;

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

代码语言:javascript
复制
//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,西-东),并计算之间的距离(以米为单位)。

代码语言:javascript
复制
//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];
票数 38
EN

Stack Overflow用户

发布于 2016-01-11 14:45:56

Hanne解决方案的快速实现:

代码语言:javascript
复制
    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)

编辑:

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

代码语言:javascript
复制
   let metersInLatitude = loc1.distance(from: loc2)
   let metersInLongitude = loc3.distance(from: loc4)
票数 19
EN

Stack Overflow用户

发布于 2021-09-10 00:25:01

基于上述,下面是我在映射项目中使用的一个有用的扩展:

代码语言:javascript
复制
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
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21273269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档