使用IBActionbutton缩放MapView

我有一个问题。 我的当前位置显示并居中在地图视图中,但地图区域不会放大。 我试图通过采取跨越和区域的didUpdateToLocation方法采取罗布的build议,但我不能实现它的权利。 我不认为这是识别我的调用setDialDialLoad和我的button不被识别。 请检查我的代码,并指出错误。 我的目标是能够使用IBActionbutton放大和缩小我的位置。

。H

- (IBAction)zoomIn:(id)sender; - (IBAction)zoomOut:(id)sender; 

在viewDidLoad中

 double miles = 0.5; MKCoordinateSpan span; span.latitudeDelta = miles/69.0; span.longitudeDelta = miles/69.0; MKCoordinateRegion region; region.span = span; [self.mapView setRegion:region animated:YES]; [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; _mapView.mapType = MKMapTypeSatellite; 

.m在我didUpdateToLocation方法。

 [self.mapView setCenterCoordinate:newLocation.coordinate animated:YES]; 

。放大:

 - (IBAction)zoomIn:(id)sender { MKCoordinateSpan span; span.latitudeDelta = _mapView.region.span.latitudeDelta * 2; span.longitudeDelta = _mapView.region.span.latitudeDelta * 2; MKCoordinateRegion region; region.span = span; region.center = _mapView.region.center; [self.mapView setRegion:region animated:YES]; } 

。缩小 :

 - (IBAction)zoomOut:(id)sender { MKCoordinateSpan span; span.latitudeDelta = _mapView.region.span.latitudeDelta / 2; span.longitudeDelta = _mapView.region.span.latitudeDelta / 2; MKCoordinateRegion region; region.span = span; region.center = _mapView.region.center; [self.mapView setRegion:region animated:YES]; } 

您可以获取当前region ,根据需要将span乘以或除以2(分割放大,缩小倍数),然后设置region

 - (IBAction)zoomIn:(id)sender { MKCoordinateRegion region = self.mapView.region; region.span.latitudeDelta /= 2.0; region.span.longitudeDelta /= 2.0; [self.mapView setRegion:region animated:YES]; } - (IBAction)zoomOut:(id)sender { MKCoordinateRegion region = self.mapView.region; region.span.latitudeDelta = MIN(region.span.latitudeDelta * 2.0, 180.0); region.span.longitudeDelta = MIN(region.span.longitudeDelta * 2.0, 180.0); [self.mapView setRegion:region animated:YES]; } 

如果你想让地图自动缩放到你的位置,你可以使用:

 self.mapView.userTrackingMode = MKUserTrackingModeFollow; 

如果您想手动执行此操作,则可以执行以下操作。 首先,定义一个类属性,以确定您是否已经放大或已经放大:

 @property (nonatomic) BOOL alreadySetZoomScale; 

然后更改您的didUpdateLocations (或didUpdateToLocation )来检查并设置缩放比例一次:

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation* newLocation = [locations lastObject]; // if less than zero, then valid lat and long not found if (newLocation.horizontalAccuracy < 0) return; if (!self.alreadySetZoomScale) { MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1609, 1609); // 1 mile = 1609.34 meters self.mapView.region = region; [self.mapView setRegion:region animated:YES]; self.alreadySetZoomScale = YES; } else { [self.mapView setCenterCoordinate:newLocation.coordinate animated:YES]; } } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // if prior to iOS 6, use this old `MKMapViewDelegate` method, but call our // other routine. if (SYSTEM_VERSION_LESS_THAN(@"6.0")) [self locationManager:manager didUpdateLocations:@[newLocation]]; } 

这基本上是这样说的,“如果我还没有放大,根据新位置和该位置周围的一定数量的米来设置区域,但是如果我已经这样做了,就根据我当前的位置设置中心坐标,但是不要改变缩放比例(在我已经放大或缩小的情况下),这也会做一些有条件的iOS版本号逻辑(使用这里显示的macros ),确保最终用户是否在6.0之前运行iOS ,它会调用我们更新的方法。

顺便说一句,如果你在地图上显示用户的位置(例如self.mapView.showsUserLocation = YES; ),你可能希望在移动地图中心之前,这个didUpdateLocations也删除与MKUserLocation关联的覆盖层,否则可以离开旧的覆盖层:

 [self.mapView removeOverlays:self.mapView.overlays]; 

尝试这个。 但是我还没有testing过。

 - (IBAction)zoomIn:(id)sender { MKCoordinateSpan span; span.latitudeDelta = _mapView.region.span.latitudeDelta * 2; span.longitudeDelta = _mapView.region.span.latitudeDelta * 2; MKCoordinateRegion region; region.span = span; region.center = _mapView.region.center; [self.mapView setRegion:region animated:YES]; } - (IBAction)zoomOut:(id)sender { MKCoordinateSpan span; span.latitudeDelta = _mapView.region.span.latitudeDelta / 2; span.longitudeDelta = _mapView.region.span.latitudeDelta / 2; MKCoordinateRegion region; region.span = span; region.center = _mapView.region.center; [self.mapView setRegion:region animated:YES]; } 

好的,这是我结束使用我的2 IBActionbutton(zoomIn和zoomOut)与我的mapView一起工作。 当你打开应用程序,用户的位置显示,地图放大和中心围绕它。 然后,您可以使用button放大或缩小2倍。*非常感谢罗布向我展示了方式。

。H

 - (IBAction)zoomIn:(id)sender; - (IBAction)zoomOut:(id)sender; 

.M

 @interface LocationViewController () @property (nonatomic) BOOL alreadySetZoomScale; @end -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if (!_alreadySetZoomScale) { MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1609, 1609); // 1 mile = 1609.34 meters self.mapView.region = region; [self.mapView setRegion:region animated:YES]; _alreadySetZoomScale = YES; } else { [self.mapView setCenterCoordinate:newLocation.coordinate animated:YES]; } - (IBAction)zoomIn:(id)sender { MKCoordinateRegion region = self.mapView.region; region.span.latitudeDelta /= 2.0; region.span.longitudeDelta /= 2.0; self.mapView.region = region; } - (IBAction)zoomOut:(id)sender {; MKCoordinateRegion region = self.mapView.region; region.span.latitudeDelta *= 2.0; region.span.longitudeDelta *= 2.0; self.mapView.region = region; } 

我有几个其他调用MKMapView像self.mapView.showsUserLocation = YES; 在viewDidLoad中,但这是非常多的。

Swift 2.0:

使用扩展:

 extension MKMapView{ func zoomInPinAnnotationLocation(targetMapViewName : MKMapView?, delta: Double) { var region: MKCoordinateRegion = targetMapViewName!.region region.span.latitudeDelta /= delta region.span.longitudeDelta /= delta targetMapViewName!.region = region } func zoomOutPinAnnotationLocation(targetMapViewName : MKMapView?,delta: Double) { var region: MKCoordinateRegion = targetMapViewName!.region region.span.latitudeDelta *= delta region.span.longitudeDelta *= delta targetMapViewName!.region = region } } 

用法:

 var mapViewZoomStepperValue: Double = -1.0 @IBOutlet weak var mapViewZoomStepper: UIStepper! @IBAction func mapViewZoomStepperValueChanged(sender: AnyObject) { if (mapViewZoomStepper.value > mapViewZoomStepperValue) { mapViewZoomStepperValue = mapViewZoomStepperValue + 1.0 //Zoom In detailMapView.zoomInPinAnnotationLocation(detailMapView, delta: 3.0) } else { mapViewZoomStepperValue = mapViewZoomStepper.value - 1.0 //Zoom Out detailMapView.zoomOutPinAnnotationLocation(detailMapView, delta: 3.0) } } 

Swift 3:

 import GoogleMaps import GooglePlaces import CoreLocation import UIKit class LocationMapView: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate { @IBOutlet weak var mapView: GMSMapView! var mapViewZoomStepperValue: Float = 0.0 @IBOutlet weak var mapViewZoomStepper: UIStepper! override func viewDidLoad() { super.viewDidLoad() mapViewZoomStepper.minimumValue = -100 } @IBAction func mapViewZoomStepperValueChanged(_ sender: Any) { if (Float(mapViewZoomStepper.value) > mapViewZoomStepperValue){ mapViewZoomStepperValue = Float(mapViewZoomStepper.value) zoomIn() }else{ mapViewZoomStepperValue = Float(mapViewZoomStepper.value) zoomOut() } } func zoomIn() { print("Zoom in!!") if mapView.camera.zoom == mapView.maxZoom { return } let currentZoom = mapView.camera.zoom + 1 mapViewZoomController(currentZoom) } func zoomOut() { print("Zoom out!!") if mapView.camera.zoom == mapView.minZoom { return } let currentZoom = mapView.camera.zoom - 1 mapViewZoomController(currentZoom) } func mapViewZoomController(_ level: Float) { let point: CGPoint = mapView.center let coor: CLLocationCoordinate2D = mapView.projection.coordinate(for: point) let camera = GMSCameraPosition.camera(withLatitude: coor.latitude, longitude: coor.longitude, zoom: Float(level)) mapView.animate(to: camera) } } 

您可以在3行代码中实现最基本的function

 var region: MKCoordinateRegion = self.mapView.region region = MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(0.005, 0.005)); self.mapView.setRegion(region, animated: true)