简体   繁体   English

MapKit iPhone显示缩放控件

[英]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? 是否可以像我一样使用MapKit在iPhone中显示地图,以便在地图上显示缩放控件?

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. 放大将当前跨度除以2,将缩小乘以2。

You change the span in the region of the mapView. 您可以在mapView的区域中更改跨度。 So you have to: 因此,您必须:

  • Get the region of the mapView. 获取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. 将mapView的区域设置为新区域。

Here's some code for suggestion 2: 这是一些建议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 但是,如果您仍然想继续进行操作,则应将按钮放置在MKMapView ,然后使用它们修改MKMapViewregion属性。

For swift 4, this is what I use: 对于Swift 4,这是我使用的方法:

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. 在我的头顶上,我不记得(在Windows框上工作)是否有一个开关来显示缩放控件。 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. 如果没有用于显示控件的开关,则需要创建一个自定义视图图层并将其放在mapkit视图的顶部。 Then you will need to call the different functions to change the zoom level. 然后,您将需要调用不同的功能来更改缩放级别。

Those functions are all documented in the mapkit docs. 这些功能都记录在mapkit文档中。 Just do a search for MapKit in the documentation center. 只需在文档中心内搜索MapKit。

Edit: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/doc/uid/TP40008205 编辑: 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. 根据文档,没有显示控件的开关,但是启用了zoom的属性使您可以打开或关闭该功能。

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. 因此,您可以订阅委托函数regionWillChange并使用mapView对象获取缩放级别,然后相应地设置图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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