26

我正在使用MKMapViewiPhone 应用程序内部。当我单击一个按钮时,缩放级别必须增加。这是我的第一种方法:

MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.5;
[mapView setRegion:zoomIn animated:YES];

但是,此代码无效,因为我没有更新 longitudeDelta 值。所以我添加了这一行:

zoomIn.span.longitudeDelta *= 0.5;

现在它起作用了,但只是有时。和不以相同的方式改变latitudeDeltalongitudeDelta我的意思是,它们的值不成比例。知道如何解决这个问题吗?

4

10 回答 10

29

我不知道这是否是正确的方法,但我正在使用它来放大和缩小。

        case 0: { // Zoom In
        //NSLog(@"Zoom - IN");
        MKCoordinateRegion region;
        //Set Zoom level using Span
        MKCoordinateSpan span;  
        region.center=mapView.region.center;

        span.latitudeDelta=mapView.region.span.latitudeDelta /2.0002;
        span.longitudeDelta=mapView.region.span.longitudeDelta /2.0002;
        region.span=span;
        [mapView setRegion:region animated:TRUE];
    }
        break;

    // Zoom Out 
    case 2: {
        //NSLog(@"Zoom - OUT");
        MKCoordinateRegion region;
        //Set Zoom level using Span
        MKCoordinateSpan span;  
        region.center=mapView.region.center;
        span.latitudeDelta=mapView.region.span.latitudeDelta *2;
        span.longitudeDelta=mapView.region.span.longitudeDelta *2;
        region.span=span;
        [mapView setRegion:region animated:TRUE];
    }
于 2009-07-29T13:58:40.757 回答
28

只是清理dkdarel的答案

// delta is the zoom factor
// 2 will zoom out x2
// .5 will zoom in by x2
- (void)zoomMap:(MKMapView*)mapView byDelta:(float) delta {

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

}

SWIFT代码:

func zoomMap(byFactor delta: Double) {
    var region: MKCoordinateRegion = self.mapView.region
    var span: MKCoordinateSpan = mapView.region.span
    span.latitudeDelta *= delta
    span.longitudeDelta *= delta
    region.span = span
    mapView.setRegion(region, animated: true)
}
于 2013-11-21T18:43:42.460 回答
23

这是一个更简单的解决方案:

MKUserLocation *userLocation = mapView.userLocation;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (
      userLocation.location.coordinate, 50, 50);
[mapView setRegion:region animated:NO];
于 2012-11-28T17:40:39.973 回答
3

mapView.setRegion旋转地图时方法有问题

您可以通过mapView.camera.altitude属性缩放地图,但它不是动画的:

mapView.camera.altitude *= 1.05

您可以创建新的相机对象并使用动画进行设置:

let currentCamera = mapView.camera
let newCamera: MKMapCamera
if #available(iOS 9.0, *) {
    newCamera = MKMapCamera(lookingAtCenter: currentCamera.centerCoordinate, fromDistance: currentCamera.altitude * 2, pitch: currentCamera.pitch, heading: currentCamera.heading)
} else {
    newCamera = MKMapCamera()
    newCamera.centerCoordinate = currentCamera.centerCoordinate
    newCamera.heading = currentCamera.heading
    newCamera.altitude = currentCamera.altitude * 2
    newCamera.pitch = currentCamera.pitch
}

mapView.setCamera(newCamera, animated: true)
于 2017-05-17T12:33:03.310 回答
2

这是我将地图移动到注释点并非常接近它缩放的方法。您可以轻松更改放大线CGFloat newLatDelta = 0.06f;

- (void)moveMapToAnnotation:(MKPointAnnotation*)annotation
{
    CGFloat fractionLatLon = map.region.span.latitudeDelta / map.region.span.longitudeDelta;
    CGFloat newLatDelta = 0.06f;
    CGFloat newLonDelta = newLatDelta * fractionLatLon;
    MKCoordinateRegion region = MKCoordinateRegionMake(annotation.coordinate, MKCoordinateSpanMake(newLatDelta, newLonDelta));
    [map setRegion:region animated:YES];
}
于 2014-04-01T10:57:31.510 回答
2

刚刚将 dkardel 的解决方案翻译成 swift:

@IBAction func zoomOutButtonClicked(sender: UIButton) {
    let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*2, longitudeDelta: mapView.region.span.longitudeDelta*2)
    let region = MKCoordinateRegion(center: mapView.region.center, span: span)

    mapView.setRegion(region, animated: true)
}

@IBAction func zoomInButtonClicked(sender: UIButton) {
    let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta/2, longitudeDelta: mapView.region.span.longitudeDelta/2)
    let region = MKCoordinateRegion(center: mapView.region.center, span: span)

    mapView.setRegion(region, animated: true)
}
于 2015-03-31T08:43:03.067 回答
1
- (IBAction)btnZoomInPressed
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    region.center.latitude = lati;
    region.center.longitude = longi;
    span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta /2.0002;
    span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta /2.0002;
    region.span=span;
    [viewMapSingleVenue setRegion:region animated:TRUE];
}

- (IBAction)btnZoomOutPressed
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    region.center.latitude = lati;
    region.center.longitude = longi;
    span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta *2;
    span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta *2;
    if(span.latitudeDelta < 200)
    {
    region.span=span;
    [viewMapSingleVenue setRegion:region animated:TRUE];
    }
}
于 2015-02-20T13:56:13.123 回答
1

我使用与您类似的代码,它似乎可以工作。可能发生的情况是您的增量变化不足以导致缩放级别从一个谷歌缩放级别增加到下一个。这也取决于地图的初始状态,这可以解释为什么它是间歇性的 - 那么在用户按下缩放按钮之前如何设置地图和缩放级别?

您还可以查看 regionThatFits 方法,该方法将调整您提供的区域(名称来自内存,因为我手边没有苹果文档)。

于 2009-06-23T12:16:37.670 回答
1

在斯威夫特 4.2

let location = mapView.userLocation
let region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 50, longitudeDelta: 50))
mapView.setRegion(region, animated: true)
于 2019-03-22T09:02:39.577 回答
0

要缩小 5%,请将区域纬度/经度增量乘以 1.05

let region = MKCoordinateRegion(center: mapView.centerCoordinate, span: MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*1.05, longitudeDelta: mapView.region.span.longitudeDelta*1.05))
self.mapView.setRegion(region, animated: true)
于 2021-05-09T07:21:42.263 回答