简体   繁体   中英

MapKit iPhone display Zoom Controls

Is it possible using MapKit to show a map in iPhone, like i´m doing, to display zoom controls on the map?

If not, what are the flags or methods i should use to increase and diminish the zoom, so i can created methods which will do this at the push of a button.

There aren't any built-in controls for it.

A couple of suggestions:

  1. You can predefine some spans and store them in an array somewhere and then store your position in that array as well. Zooming in/out changes the position and gets the span.
  2. Zooming in takes the current span and divides by 2, zooming out multiplies by 2.

You change the span in the region of the mapView. So you have to:

  • Get the region of the mapView.
  • Get the span of the region.
  • Change the span to what you want.
  • Set the regions span to the new span.
  • Set the mapView's region to the new region.

Here's some code for suggestion 2:

MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
span.latitudeDelta = region.span.latitudeDelta*2;
span.longitudeDelta = region.span.longitudeDelta*2;
region.span = span;
[mapView setRegion:region animated:TRUE];

I don't think you should do that, the expected way to zoom in and out is using the pinch gestures. However if you still want to go ahead and do it, you should place yourself the buttons over the MKMapView , and then with those modify the region property of the MKMapView

For swift 4, this is what I use:

func zoom(_ zoomin : Bool) {
    var region = mapView.region;
    var span = MKCoordinateSpan();
    span.latitudeDelta = zoomin ? region.span.latitudeDelta / 2 :  region.span.latitudeDelta * 2;
    span.longitudeDelta = zoomin ? region.span.longitudeDelta / 2 : region.span.longitudeDelta * 2;
    region.span = span;

    mapView.setRegion(region, animated: true);
}

Off the top of my head I don't remember (at work on a windows box) if there is a switch to display the zoom controls. I assume you are talking about displaying the level of zoom, because by default you can pinch/spread the view for zoom.

If there isn't a switch to display the controls, then you will need to create a custom view layer and put it on top of the mapkit view. Then you will need to call the different functions to change the zoom level.

Those functions are all documented in the mapkit docs. Just do a search for MapKit in the documentation center.

Edit: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/doc/uid/TP40008205

According to the docs there isn't a switch to display the controls, but the zoom enabled property lets you turn the ability on and off.

So you can subscribe to the delegate function regionWillChange and use the mapView object to get the zoom level and then set your graphics accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM