【问题标题】:Cannot get UIMapView to "zoom"/setRegion: when the map is loaded for the first time无法让 UIMapView “缩放”/setRegion:第一次加载地图时
【发布时间】:2012-10-22 18:28:32
【问题描述】:

我有一个UIViewController,它加载了一张地图,上面已经有一堆图钉。让图钉出现没有问题,但是当用户第一次到达 mapView 时,我似乎无法让MKMapView 执行 setRegion。我尝试将代码放入所有可行的方法中,viewDidLoadviewWillLoadmapViewWillStartLoadingMapmapViewDidFinishLoadingMap 等,但无济于事。

然而,如果我返回前一个 viewController 然后再次进入包含 mapView 的 viewContoller,它将“缩放”到指定区域。

有没有我不知道的超级秘密方法?调用[mapView setRegion:(...) animated:YES]; 不应该足以让地图“缩放”/更新以显示所选区域吗??

另外,如果有人知道如何获取图钉(我使用MKPointAnnotation 为我的)以动画下降以及让地图以动画放大,(两者都没有这样做,尽管我将两者都设置为动画......),我也会很感激这些信息。

谢谢!

代码:

@implementation FindOnMapMapView
@synthesize mapView;
@synthesize mapViewTabBarItem;
@synthesize results;
@synthesize reverseGeocoder;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake([defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]), MKCoordinateSpanMake(0.744494189288569, 0.744494189288569));

        mapView.region = mapRegion;
        [mapView setRegion:mapRegion animated:YES];



NSMutableArray *annotations = [[NSMutableArray alloc] init];
        for(ThePlace *place in results)
        {
            MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init];

            [addAnnotation setCoordinate:CLLocationCoordinate2DMake(place.latitude, place.longitude)];

            addAnnotation.title = place.placename;
            addAnnotation.subtitle = [NSString stringWithFormat:@"Distance: %@", place.distance];

            [mapView addAnnotation:addAnnotation];

        }

        [self.mapView addAnnotations:annotations ];
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake([defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]), MKCoordinateSpanMake(0.744494189288569, 0.744494189288569));
        NSLog(@"Region Coords are: Latitude:%f, Longitude:%f", [defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]);
        mapView.region = mapRegion;
        [mapView setRegion:mapRegion animated:YES];
    }

【问题讨论】:

  • @J2theC 按要求添加了代码
  • 当这个视图控制器第一次显示时,NSLog 打印的坐标是什么?首次显示时,地图是否显示大西洋?如果您将坐标硬编码到 setRegion 中(而不是从 NSUserDefaults 中读取),它会起作用吗? NSUserDefaults setDouble 调用在哪里完成?
  • @AnnaKarenina IT 打印了正确的坐标,(我刚刚检查了它们)。是的,它显示的位置是在北大西洋,我将它设置在我的主视图控制器中,这是后面的几个场景,第一次加载,硬编码以相同的结果结束,第一次加载到北大西洋,最后,我在让 CLLocationManager 为我提供“用户”(我自己的)坐标之前遇到问题,直到我将代码放在我的第一个视图(主菜单)中,这与我从 NSUSerDefaults 检索坐标的 mapView 相距几个segues。

标签: iphone ios mkmapview mapkit


【解决方案1】:
- (void) viewDidAppear:(BOOL)animated {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(firstPoint.coordinate, 2000, 2000);
    [mapView setRegion:region animated:YES];
}

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    相关资源
    最近更新 更多