使用IBAction按钮缩放MapView [英] Using IBAction Buttons to Zoom MapView

查看:124
本文介绍了使用IBAction按钮缩放MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an issue. My current location is displayed and centered in a map view however the map region doesn't get zoomed in to. I tried taking Rob's advice by taking span and region out of the didUpdateToLocation method but I must not have implemented it right. I don't think it's recognizing my call to setRegion in viewDidLoad and my buttons aren't being recognized. Please check my code below and point out the mistake(s). My goal is to be able to zoom in and out of my location using the IBAction buttons.

.h

- (IBAction)zoomIn:(id)sender;

- (IBAction)zoomOut:(id)sender;

.m in viewDidLoad

.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。

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

.Zoom In:

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

.Zoom Out:

.Zoom Out :

- (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(除以放大,乘以缩小),以及然后设置区域

You can get the current region, multiply or divide the span by two, as appropriate (dividing on zoom in, multiplying on zoom out), and then set the 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];
}






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


If you want to have the map automatically zoom to your location, you can use:

self.mapView.userTrackingMode = MKUserTrackingModeFollow;

如果您想手动执行此操作,您可以执行以下操作。首先,为你是否已经放大了一个类属性:

If you want to do this manually, you can do something like the following. First, define an class property for whether you've zoomed in already or not:

@property (nonatomic) BOOL alreadySetZoomScale;

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

then change your didUpdateLocations (or didUpdateToLocation) to check this and set the zoom scale once:

- (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,它将调用我们更新的方法。

This basically says, "if I haven't zoomed in yet, set the region based upon the newLocation and a certain number of meters around that location, but if I have already done that, the just set the center coordinate based upon my current location, but don't change the zoom scale (in case I already zoomed in or out). This also does some conditional iOS version number logic (using the macros shown here), making sure if the end-user is running iOS prior to 6.0, that it will call our updated method.

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

By the way, if you're showing the user location on the map (e.g. self.mapView.showsUserLocation = YES;), you might want to have this didUpdateLocations also remove the overlay associated with the MKUserLocation before moving the map center, otherwise it can leave the old overlay sitting around:

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

这篇关于使用IBAction按钮缩放MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆