将跨度值转换为地图视图上的米

发布于 2024-10-21 20:22:18 字数 651 浏览 1 评论 0原文

每当用户放大或缩小地图时,我需要知道地图上当前显示了多少米(宽度或高度)。

我需要的是 MKCooperativeRegionMakeWithDistance 的反函数来计算当前地图跨度表示的距离。

我尝试了以下代码,但得到了错误的结果:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    MKMapRect mRect = self.map.visibleMapRect;
    MKMapPoint northMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMinY(mRect));
    MKMapPoint southMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMaxY(mRect));

    self.currentDist = MKMetersBetweenMapPoints(northMapPoint, southMapPoint);
}

如果我将地图区域设置为 1500 米,结果会得到类似 1800 的结果。

感谢您的帮助, 文森特

Whenever the user zoom in or out the map i need to know how many meters are currently represented on the map (width or height).

What i need is the inverse function of MKCoordinateRegionMakeWithDistance to calculate the distance represented by the current map span.

I tried the following code but i get wrong results :

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    MKMapRect mRect = self.map.visibleMapRect;
    MKMapPoint northMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMinY(mRect));
    MKMapPoint southMapPoint = MKMapPointMake(MKMapRectGetMidX(mRect), MKMapRectGetMaxY(mRect));

    self.currentDist = MKMetersBetweenMapPoints(northMapPoint, southMapPoint);
}

If i set the map region to 1500 meters i get something like 1800 as a result..

Thanks for your help,
Vincent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

洛阳烟雨空心柳 2024-10-28 20:22:18

实际上这是一个非常愚蠢的错误,如果我沿 X 轴执行相同的操作,那么我会得到正确的值:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
MKMapRect mRect = self.map.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));

self.currentDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
}

Actually it was a really stupid mistake, if i do the same operation along the X axis then i get the correct value :

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
MKMapRect mRect = self.map.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));

self.currentDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
}
帅气称霸 2024-10-28 20:22:18
- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated {
  MKCoordinateSpan span = mapView.region.span;
  NSLog(@" 1 = ~111 km -> %f = ~ %f km ",span.latitudeDelta,span.latitudeDelta*111);
}

根据文档 http://developer.apple.com/library /ios/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html,

latitudeDelta

要在地图上显示的南北距离(以度为单位)。与根据纬度而变化的纵向距离不同,纬度一度始终约为 111 公里(69 英里)。

- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated {
  MKCoordinateSpan span = mapView.region.span;
  NSLog(@" 1 = ~111 km -> %f = ~ %f km ",span.latitudeDelta,span.latitudeDelta*111);
}

According to the docs http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html,

latitudeDelta

The amount of north-to-south distance (measured in degrees) to display on the map. Unlike longitudinal distances, which vary based on the latitude, one degree of latitude is always approximately 111 kilometers (69 miles).

叶落知秋 2024-10-28 20:22:18

感谢大家的发帖。我有一个应用程序需要一英里半径才能计算出要获取多少位置记录,因此这很方便。对于将来可能遇到此问题的任何人来说,这是快速等效的。

let mRect: MKMapRect = self.mapView.visibleMapRect
        let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect))
        let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect))
        let currentDistWideInMeters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
        let milesWide = currentDistWideInMeters / 1609.34  // number of meters in a mile
        println(milesWide)

Thanks for the post all. I have an app that required a mile radius to figure out how many location records to fetch so this came in handy. Here is the swift equivalent for anyone who might come across this in the future.

let mRect: MKMapRect = self.mapView.visibleMapRect
        let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect))
        let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect))
        let currentDistWideInMeters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
        let milesWide = currentDistWideInMeters / 1609.34  // number of meters in a mile
        println(milesWide)
-小熊_ 2024-10-28 20:22:18

斯威夫特4.2

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

        let mapRect = mapView.visibleMapRect
        let westMapPoint = MKMapPoint(x: mapRect.minX, y: mapRect.midY)
        let eastMapPoint = MKMapPoint(x: mapRect.maxX, y: mapRect.midY)
        let visibleDistance = westMapPoint.distance(to: eastMapPoint)
    }

Swift 4.2

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

        let mapRect = mapView.visibleMapRect
        let westMapPoint = MKMapPoint(x: mapRect.minX, y: mapRect.midY)
        let eastMapPoint = MKMapPoint(x: mapRect.maxX, y: mapRect.midY)
        let visibleDistance = westMapPoint.distance(to: eastMapPoint)
    }
强辩 2024-10-28 20:22:18

这是一种更简单的方法(以米为单位获取宽度和高度)......

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    MKMapRect rect = mapView.visibleMapRect;

    double mapWidth = MKMapRectGetWidth(rect) / 10;
    double mapHeight = MKMapRectGetHeight(rect) / 10;

}

Here's an easier way (to get width and height in meters)...

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    MKMapRect rect = mapView.visibleMapRect;

    double mapWidth = MKMapRectGetWidth(rect) / 10;
    double mapHeight = MKMapRectGetHeight(rect) / 10;

}
乖乖公主 2024-10-28 20:22:18

雨燕4

extension MKMapView {
  func regionInMeter() -> CLLocationDistance {
    let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))
    let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))

    return MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
  }
}

Swift 4

extension MKMapView {
  func regionInMeter() -> CLLocationDistance {
    let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))
    let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(visibleMapRect), MKMapRectGetMidY(visibleMapRect))

    return MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文