Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether zoom level changed

Tags:

I'm using MapKit on iPhone. How can I know when the user changes the zoom level (zoom in\out the map)?

I've tried to use mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; but that's called even when the map is only dragged. Unfortunately, when the map is dragged the mapView.region.span changes as well...

Help?

10x

like image 366
Rizon Avatar asked Dec 05 '10 15:12

Rizon


People also ask

How do I check my browser zoom level?

One way to detect the browser zoom level is to use the window. devicePixelRatio property. When we zoom in or out, the resize event will be triggered.

How do I check my browser zoom level in Chrome?

If you are using a mouse, you can hold down the keyboard Ctrl key and use the mouse wheel to zoom in or out. When you do this, you will see an icon on the right side of the address bar, indicating the zoom level has deviated from the default.

Can Javascript detect the browser zoom level?

Currently there is no way to detect the zoom level with Javascript reliably.

How do I zoom the same element size?

min-width: 100%; This will freeze the width, you can do the same for height too.


1 Answers

It is pretty simple to calculate the zoom level. See the snippet below. You can get the mRect parameter from the visibleMapRect property on your MKMapView instance.

+ (NSUInteger)zoomLevelForMapRect:(MKMapRect)mRect withMapViewSizeInPixels:(CGSize)viewSizeInPixels {     NSUInteger zoomLevel = MAXIMUM_ZOOM; // MAXIMUM_ZOOM is 20 with MapKit     MKZoomScale zoomScale = mRect.size.width / viewSizeInPixels.width; //MKZoomScale is just a CGFloat typedef     double zoomExponent = log2(zoomScale);     zoomLevel = (NSUInteger)(MAXIMUM_ZOOM - ceil(zoomExponent));     return zoomLevel; } 

You could probably just stop at the step for calculating the zoomScale as that will tell you if the zoom has changed at all.

I figured this stuff out from reading Troy Brants excellent blog posts on the topic:

http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/

Swift 3

extension MKMapView {      var zoomLevel: Int {         let maxZoom: Double = 20         let zoomScale = self.visibleMapRect.size.width / Double(self.frame.size.width)         let zoomExponent = log2(zoomScale)         return Int(maxZoom - ceil(zoomExponent))     }  } 
like image 137
Aaron Avatar answered Oct 14 '22 18:10

Aaron