如何找到 MKMapView 当前的缩放级别?

发布于 2024-12-07 13:36:18 字数 234 浏览 1 评论 0原文

我想知道iphone编程中MKMapView当前的缩放级别,我该怎么做? 实际上,我有一个应用程序,它需要几个参数(从 MKMapView 中心到角的半径)返回商店详细信息,当我在 MKMapView 上时,半径非常高,当半径较小时它会发生变化,所以我想知道缩放级别并根据该级别设置我的网络服务,如何获取当前 MKMapView 可见区域的缩放级别?

I want to know the current zoom level of MKMapView in iphone programming, how can I do that?
Actually I have one app, that is taking few arguments(Radius from center to corner of MKMapView) are returning the store details in that are, when I am on MKMapView, the radius is very high and it changes when Radius is smaller, so I want to know the zoom level and setup my webservice according to that, How can I get zoom level of current MKMapView visible area?

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

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

发布评论

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

评论(5

不离久伴 2024-12-14 13:36:18

我为其创建了非常简单的辅助子类:

#define MERCATOR_RADIUS 85445659.44705395
#define MAX_GOOGLE_LEVELS 20

@interface MKMapView (ZoomLevel)
- (double)getZoomLevel;
@end

@implementation MKMapView (ZoomLevel)

- (double)getZoomLevel
{
    CLLocationDegrees longitudeDelta = self.region.span.longitudeDelta;
    CGFloat mapWidthInPixels = self.bounds.size.width;
    double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
    double zoomer = MAX_GOOGLE_LEVELS - log2( zoomScale );
    if ( zoomer < 0 ) zoomer = 0;
//  zoomer = round(zoomer);
    return zoomer;
}

@end

I created very simple helper subclass for it:

#define MERCATOR_RADIUS 85445659.44705395
#define MAX_GOOGLE_LEVELS 20

@interface MKMapView (ZoomLevel)
- (double)getZoomLevel;
@end

@implementation MKMapView (ZoomLevel)

- (double)getZoomLevel
{
    CLLocationDegrees longitudeDelta = self.region.span.longitudeDelta;
    CGFloat mapWidthInPixels = self.bounds.size.width;
    double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);
    double zoomer = MAX_GOOGLE_LEVELS - log2( zoomScale );
    if ( zoomer < 0 ) zoomer = 0;
//  zoomer = round(zoomer);
    return zoomer;
}

@end
对岸观火 2024-12-14 13:36:18

您可以在 MKMapViewregion 属性中使用 span。它的定义如下:

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
} MKCoordinateSpan;

看看 文档。那里有很好的解释。

You can use span inside the region property of the MKMapView. It is defined like this:

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
} MKCoordinateSpan;

Take a look at the documentation. It is well explained there.

蓝颜夕 2024-12-14 13:36:18

之前的所有答案都没有考虑当前的地图旋转。 MKMapView 的 longitudeDelta 对于非旋转地图和旋转地图是不同的。
这是一个用于直接地图缩放计算的很棒的函数: https://stackoverflow.com/a/15020534/4923516

这里是我对 Swift 的改进,它考虑了地图旋转并返回当前缩放级别:

class MyMapView : MKMapView {

  func getZoom() -> Double {
    // function returns current zoom of the map
    var angleCamera = self.camera.heading
    if angleCamera > 270 {
        angleCamera = 360 - angleCamera
    } else if angleCamera > 90 {
        angleCamera = fabs(angleCamera - 180)
    }
    let angleRad = M_PI * angleCamera / 180 // camera heading in radians
    let width = Double(self.frame.size.width)
    let height = Double(self.frame.size.height)
    let heightOffset : Double = 20 // the offset (status bar height) which is taken by MapKit into consideration to calculate visible area height
    // calculating Longitude span corresponding to normal (non-rotated) width
    let spanStraight = width * self.region.span.longitudeDelta / (width * cos(angleRad) + (height - heightOffset) * sin(angleRad))
    return log2(360 * ((width / 256) / spanStraight)) + 1;
  }

}

您可以在我的存储库中下载示例项目:https://github.com/d-babych/mapkit-wrap

All the previous answers do not take into consideration current map rotation. MKMapView's longitudeDelta differs for non-rotated map and rotated map.
Here is a great function for straight map zoom calculation: https://stackoverflow.com/a/15020534/4923516

And here is my improvement in Swift, that takes into consideration map rotation and returns current zoom level:

class MyMapView : MKMapView {

  func getZoom() -> Double {
    // function returns current zoom of the map
    var angleCamera = self.camera.heading
    if angleCamera > 270 {
        angleCamera = 360 - angleCamera
    } else if angleCamera > 90 {
        angleCamera = fabs(angleCamera - 180)
    }
    let angleRad = M_PI * angleCamera / 180 // camera heading in radians
    let width = Double(self.frame.size.width)
    let height = Double(self.frame.size.height)
    let heightOffset : Double = 20 // the offset (status bar height) which is taken by MapKit into consideration to calculate visible area height
    // calculating Longitude span corresponding to normal (non-rotated) width
    let spanStraight = width * self.region.span.longitudeDelta / (width * cos(angleRad) + (height - heightOffset) * sin(angleRad))
    return log2(360 * ((width / 256) / spanStraight)) + 1;
  }

}

You can download sample project in my repo: https://github.com/d-babych/mapkit-wrap

江挽川 2024-12-14 13:36:18

获取当前缩放级别的整数的最简单方法是使用 MapView 函数:regionDidChangeAnimated。此功能可识别变焦的每次变化,并为您提供计算变焦系数的基础。

只需将此函数插入​​到您的 MapView 类中(适用于 Swift 3.0):

var mapView: MKMapView! = nil

...

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let zoomWidth = mapView.visibleMapRect.size.width
    let zoomFactor = Int(log2(zoomWidth)) - 9
    print("...REGION DID CHANGE: ZOOM FACTOR \(zoomFactor)")
}

您将获得一个 ZoomFactor 值,其中 0 是您可以放大地图的最近点以及每个更高的值是一个很远很远的变焦...:-)

The easiest way to get an Integer of the current zoom level, is by using the MapView function: regionDidChangeAnimated. This function recognizes every change in zoom and will give you the basis for the calculation of the zoom factor.

Just insert this function into your MapView class (works for Swift 3.0):

var mapView: MKMapView! = nil

...

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    let zoomWidth = mapView.visibleMapRect.size.width
    let zoomFactor = Int(log2(zoomWidth)) - 9
    print("...REGION DID CHANGE: ZOOM FACTOR \(zoomFactor)")
}

And you will get a zoomFactor value out of it, where 0 is the most near point you can zoom into the map and every higher value is a far far away zoom... :-)

飘过的浮云 2024-12-14 13:36:18

如果有帮助,您还可以使用 mapView.camera.altitude 获取当前显示的地图区域的当前海拔高度。

当我搜索如何获取地图缩放级别时,这对我很有帮助。


最新语法:

let zoomage = mapView.camera.centerCoordinateDistance

If it helps you can also use mapView.camera.altitude to get the current altitude of the currently displayed map region.

This is what helped me when I searched how to get the map zoom level.


Latest syntax:

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