MKMapView: Getting the relative zoom ratio?

天大地大妈咪最大 提交于 2020-03-15 03:27:28

问题


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.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2960294/mkmapview-getting-the-relative-zoom-ratio

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