7

問題があります。現在地が地図ビューの中央に表示されますが、地図の領域は拡大されません。didUpdateToLocation メソッドからスパンとリージョンを取り出すことで、ロブのアドバイスを取り入れようとしましたが、正しく実装していなかったに違いありません。viewDidLoad での setRegion への呼び出しが認識されていないと思いますが、ボタンが認識されていません。以下のコードを確認し、間違いを指摘してください。私の目標は、IBAction ボタンを使用して現在地をズームインおよびズームアウトできるようにすることです。

.h

- (IBAction)zoomIn:(id)sender;

- (IBAction)zoomOut:(id)sender;

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

6 に答える 6

10

現在の を取得し、必要に応じて を 2regionで乗算または除算しspan(ズームインで除算、ズームアウトで乗算)、次に を設定できます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) を変更してこれを確認し、ズーム スケールを 1 回設定します。

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

OK、これは、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;

}

self.mapView.showsUserLocation=YESのようなMKMapViewへの他の呼び出しがいくつかあります。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 に答える