首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IBAction按钮缩放MapView

使用IBAction按钮缩放MapView
EN

Stack Overflow用户
提问于 2013-03-03 01:34:07
回答 6查看 5.4K关注 0票数 7

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

.h

代码语言:javascript
复制
- (IBAction)zoomIn:(id)sender;

- (IBAction)zoomOut:(id)sender;

viewDidLoad中的.m

代码语言:javascript
复制
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;

我的didUpdateToLocation方法中的.m。

代码语言:javascript
复制
[self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];

.Zoom In:

代码语言:javascript
复制
- (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输出:

代码语言:javascript
复制
- (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];
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-03-03 03:28:52

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

代码语言:javascript
复制
- (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];
}

如果您希望地图自动缩放到您的位置,您可以使用:

代码语言:javascript
复制
self.mapView.userTrackingMode = MKUserTrackingModeFollow;

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

代码语言:javascript
复制
@property (nonatomic) BOOL alreadySetZoomScale;

然后更改didUpdateLocations (或didUpdateToLocation)以选中此选项,并设置缩放比例一次:

代码语言:javascript
复制
- (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版本号逻辑(使用macros shown here),以确保最终用户运行的是iOS 6.0之前的版本,它将调用我们的更新方法。

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

代码语言:javascript
复制
[self.mapView removeOverlays:self.mapView.overlays];
票数 10
EN

Stack Overflow用户

发布于 2013-03-03 02:13:51

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

代码语言:javascript
复制
- (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];

 }
票数 1
EN

Stack Overflow用户

发布于 2013-03-07 08:51:51

好了,这是我最后用来让我的两个IBAction按钮(zoomIn和zoomOut)与我的mapView一起工作的东西。打开应用程序时,将显示用户位置,地图将放大并围绕其居中。然后你可以使用按钮放大或缩小2倍。*非常感谢Rob给我指路。

.h

代码语言:javascript
复制
- (IBAction)zoomIn:(id)sender;

- (IBAction)zoomOut:(id)sender;

.m

代码语言:javascript
复制
@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;

}

在viewDidLoad中,我还有其他几个对MKMapView的调用,比如self.mapView.showsUserLocation = YES;,但差不多就这些了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15177400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档