MKMapView: Getting the relative zoom ratio?

2019-02-19 22:57发布

I need to get some kind of scaling factor which tells me by how much the map has zoomed (whenever the region changes).

My first idea was to use the span's delta latitude or longitude values. I would keep a track of the 'old' value and then update the value when the map region changes and compare the ratio with the old value.

I'm using the following delegate method:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    // Get the new value
    CLLocationDegrees newdlat = mapView.region.span.latitudeDelta;

    // Find the zoom ratio (use longitude instead?)
    CGFloat zoomRatio = newdlat / olddlat;

    // Update the old value (defined elsewhere)
    olddlat = newdlat;
}

I'm not using the longitude delta in this case because I figure it would scale proportionally to the latitude which would yield the same ratio (is this wrong?).

The problem is when I check the values of the longitude and latitude deltas while scrolling the map (without zooming) the values keep changing slightly. Shouldn't the span values stay the same if the map isn't zooming? Maybe I misunderstood something.

Due to this little variation I'm worried that my zoom ratio won't be accurate enough to rely on (ultimately I'm going to use it to scale annotation images with the map).

My main question is is this an good approach or is there a much simpler way that I'm overlooking?

Thanks for any help.

3条回答
成全新的幸福
2楼-- · 2019-02-19 23:32

The Earth is a sphere. A map is a plane.

To map the Earth on a plane, some distortion is necessary.

In the Mercator projection the lines of longitude are not distorted but the lines of latitude appear curved.

Hence an increment in longitude will involve a constant value whereas the same is not true for an increment in latitude. Hence "scale" in this case (degrees/mm) depends on the orientation of the measuring stick.

查看更多
干净又极端
3楼-- · 2019-02-19 23:37

The problem was resolved by using longitude instead of latitude when calculating the ratio. See my two comments to the original post for details on why this is the case.

查看更多
ら.Afraid
4楼-- · 2019-02-19 23:42

In the Breadcrumb sample code the compute MKZoomScale using the following approach.

MKZoomScale currentZoomScale = mapView.bounds.size.width / mapView.visibleMapRect.size.width;

http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Listings/Classes_BreadcrumbViewController_m.html

查看更多
登录 后发表回答