7

我有一个问题。我的当前位置在地图视图中显示并居中,但地图区域不会放大。我尝试通过从 didUpdateToLocation 方法中取出 span 和 region 来采纳 Rob 的建议,但我一定没有正确实施它。我认为它没有识别我在 viewDidLoad 中对 setRegion 的调用,并且我的按钮没有被识别。请检查我下面的代码并指出错误。我的目标是能够使用 IBAction 按钮放大和缩小我的位置。

。H

- (IBAction)zoomIn:(id)sender;

- (IBAction)zoomOut:(id)sender;

.m 在 vi​​ewDidLoad

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];
}
4

6 回答 6

10

您可以根据需要获取当前值region、乘以或除以span2(放大时除,缩小时乘以),然后设置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]];
}

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

顺便说一句,如果您在地图上显示用户位置(例如self.mapView.showsUserLocation = YES;),您可能希望在移动地图中心之前didUpdateLocations也删除与 关联的叠加层MKUserLocation,否则它可能会留下旧的叠加层:

[self.mapView removeOverlays:self.mapView.overlays];
于 2013-03-02T19:28:51.960 回答
1

试试这个。但我还没有测试。

- (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];

 }
于 2013-03-02T18:13:51.360 回答
1

好的,这就是我最终用来让我的 2 个 IBAction 按钮(zoomIn 和 zoomOut)与我的 mapView 一起工作的方法。当您打开应用程序时,会显示用户位置,并且地图会放大并以它为中心。然后,您可以使用按钮放大或缩小 2 倍。*非常感谢 Rob 为我指明了方向。

。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 中,但仅此而已。

于 2013-03-07T00:51:51.053 回答
0

斯威夫特 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)
  }

 }
于 2016-03-29T11:03:23.903 回答
0

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

var region: MKCoordinateRegion = self.mapView.region
region = MKCoordinateRegionMake(self.mapView.centerCoordinate, MKCoordinateSpanMake(0.005, 0.005));
self.mapView.setRegion(region, animated: true)
于 2016-09-04T05:55:58.640 回答
0

斯威夫特 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)

        }

}
于 2017-07-19T07:42:25.333 回答