MKMapView setRegion:异常行为? [英] MKMapView setRegion: odd behavior?

查看:58
本文介绍了MKMapView setRegion:异常行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个带有3个选项卡的iPhone应用程序.在这里,我希望每个选项卡的视图看起来像它共享同一张地图.因此,目前,我只是想弄清楚在单击新选项卡时如何重置每个视图的MKMapView的区域. [换句话说,如果您在选项卡1上并一直放大到某个位置,则单击选项卡2,我希望它显示相同的放大地图.]

I'm working on a 3-tabbed iPhone app., where I want each tab's view to look like it shares the same map. So at the moment, I'm just trying to figure out how to reset each view's MKMapView's region when one clicks on a new tab. [In other words, if you're on tab 1 and zoomed all the way in on a spot, then click on tab 2, I want it to show the same, zoomed in map.]

为此,我在应用程序委托中有一个MKCoordinateRegion,每个视图在消失之前都保存到该视图,并且每个新视图在查看之前都从中读取.

To do this, I have an MKCoordinateRegion in the app delegate that each view saves to before it goes away, and that each new view reads from before it is viewed.

[换句话说,我的应用程序委托是我的TabBarControllerDelegate,并且我已经覆盖了tabBarController:shouldSelectViewController:.在有人单击选项卡按钮之后,并且在新选项卡的视图的viewWillAppear:函数被调用之前,将调用此函数.因此,在此函数中,我将当前视图的区域保存到AppDelegate :: MKCoordinateRegion变量中,然后调用新视图的viewWillAppear :(或viewDidLoad :).然后,在新视图的viewWillAppear:中,我从应用程序委托获取MKCoordinateRegion,并将其分配给新视图的区域.]

[In other words, my app delegate is my TabBarControllerDelegate and I've overridden tabBarController: shouldSelectViewController:. This function is called after someone clicks on a tab button, and before the new tab's view's viewWillAppear: function is called. So, in this function, I save the region of the current view to my AppDelegate::MKCoordinateRegion variable, and then let the new view's viewWillAppear: (or viewDidLoad:) get called. Then, in the new view's viewWillAppear:, I get the MKCoordinateRegion from the app delegate, and assign it to my new view's region.]

但是,MKMapKit :: setRegion:中的值似乎不一致,并且我不知道我是在做错什么,完全遗漏了什么,还是其他事情正在发生.

However, the values from MKMapKit::setRegion: don't seem to be consistent and I don't know if I'm just doing something wrong, missing something entirely, or if something else is going on.

我只从事iPhone开发. (以及Obj-c等)大约2星期了,所以这很好可能只是一个新的错误.如果是这样,我将继续浪费您的时间,并向您道歉.尽管如此,这还是一些代码(我留在了我正在使用的注释中,以防万一您自己尝试运行它):

I've only been doing iPhone dev. (and Obj-c, etc.) for about 2 weeks now, so this very well just could be a newby mistake. And if so, I will go ahead and apologize now for wasting your time. Nonetheless, here's some code (and I've left in the comments I was using...just in case you try running it yourself):

iPhoneTestAppDelegate.h

iPhoneTestAppDelegate.h

...
@interface iPhoneTestAppDelegate : NSObject  
{
    UIWindow* _window;
    UITabBarController* _tabBarController;
    SubViewController* _subViewController;

    MKCoordinateRegion _mapRegion;
}

@property (nonatomic, retain) IBOutlet UIWindow* window;
@property (nonatomic, retain) IBOutlet UITabBarController* tabBarController;
@property (nonatomic, retain) IBOutlet SubViewController* subViewController;
@property (nonatomic, assign) MKCoordinateRegion mapRegion;

@end 

iPhoneAppDelegate.m

iPhoneAppDelegate.m

...
static CLLocationDegrees INITIAL_LATITUDE = 40.754019;
static CLLocationDegrees INITIAL_LONGITUDE = -73.973351;
static CLLocationDegrees INITIAL_SPAN_LAT_DEG = .10767;
static CLLocationDegrees INITIAL_SPAN_LONG_DEG = .109863;
...
- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
    NSLog(@"Begin");
    // initialize the delegate's region
    MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
    region.center.latitude = INITIAL_LATITUDE;
    region.center.longitude = INITIAL_LONGITUDE;
    region.span.latitudeDelta = INITIAL_SPAN_LAT_DEG;
    region.span.longitudeDelta = INITIAL_SPAN_LONG_DEG;
    self.mapRegion = region;

    self.tabBarController.delegate = self;
    [self.window addSubview:self.tabBarController.view];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController
 shouldSelectViewController:(UIViewController *)viewController
{
 NSLog(@"in UITabBarControllerDelegate: shouldSelectViewController:");

 if([tabBarController.viewControllers indexOfObject:viewController] !=
  [tabBarController selectedIndex])
 {
  NSLog(@"different view selected");
  BaseViewController* currView = (BaseViewController *)[tabBarController selectedViewController];

  NSLog(@"%f %f %f %f", 
     self.mapRegion.center.latitude, self.mapRegion.center.longitude, 
     self.mapRegion.span.latitudeDelta, self.mapRegion.span.longitudeDelta);
  NSLog(@"%f %f %f %f", 
     currView.mapView.region.center.latitude, currView.mapView.region.center.longitude, 
     currView.mapView.region.span.latitudeDelta, currView.mapView.region.span.longitudeDelta);
  self.mapRegion = currView.mapView.region;
  NSLog(@"%f %f %f %f", 
     self.mapRegion.center.latitude, self.mapRegion.center.longitude, 
     self.mapRegion.span.latitudeDelta, self.mapRegion.span.longitudeDelta);
 }
 else
  NSLog(@"same view selected");

 return YES;
}

BaseViewController.m

BaseViewController.m

...
- (void)viewDidLoad 
{
    [super viewDidLoad];
 NSLog(@"Base: viewDidLoad");

 iPhoneTestAppDelegate* appDelegate = (iPhoneTestAppDelegate *)
  [[UIApplication sharedApplication] delegate];
 [self.mapView setDelegate:self];
    //[self.mapView setRegion:appDelegate.mapRegion animated:YES];
 [self.mapView setRegion:[self.mapView regionThatFits:appDelegate.mapRegion]];

 NSLog(@"---------------------");
 NSLog(@"%f %f %f %f", 
    appDelegate.mapRegion.center.latitude, appDelegate.mapRegion.center.longitude, 
    appDelegate.mapRegion.span.latitudeDelta, appDelegate.mapRegion.span.longitudeDelta);
 NSLog(@"---------------------");
 NSLog(@"%f %f %f %f", 
    self.mapView.region.center.latitude, self.mapView.region.center.longitude, 
    self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
 NSLog(@"---------------------");  
}

- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];
 NSLog(@"Base: viewWillAppear");

 iPhoneTestAppDelegate* appDelegate = (iPhoneTestAppDelegate *)
  [[UIApplication sharedApplication] delegate];  
 //[self.mapView setRegion:appDelegate.mapRegion animated:YES];
 [self.mapView setRegion:[self.mapView regionThatFits:appDelegate.mapRegion]];

 NSLog(@"*********************");
 NSLog(@"%f %f %f %f", 
    appDelegate.mapRegion.center.latitude, appDelegate.mapRegion.center.longitude, 
    appDelegate.mapRegion.span.latitudeDelta, appDelegate.mapRegion.span.longitudeDelta);
 NSLog(@"*********************");
 NSLog(@"%f %f %f %f", 
    self.mapView.region.center.latitude, self.mapView.region.center.longitude, 
    self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
 NSLog(@"*********************");
}
...

现在,我在运行此命令时首先注意到的是,当调用初始视图的viewDidLoad:时,委托的区域和我的新视图的区域是相同的(在我调用setRegion:之后).但是,在viewWillAppear:中调用setRegion:之后,我的视图的MKMapView的区域不再等于委托.不知何故,当在viewWillAppear:中运行时,viewDidLoad:中完全相同的代码会产生不同的结果!不是这样吗?为什么从viewWillAppear中调用setRegion:来更改视图的MKMapView区域的值?这与regionThatFits有关:在内部被调用吗?我不明白!

Now, the first thing I notice when I run this is that when the initial view's viewDidLoad: is called, the delegate's region and my new view's region are the same (after I call setRegion:). However, after setRegion: is called in viewWillAppear:, my view's MKMapView's region is no longer equal to the delegates. Somehow, the exact same code in viewDidLoad: produces a different result when run in viewWillAppear:! Shouldn't that not be the case? Why does calling setRegion: from viewWillAppear: change the values of my view's MKMapView's region? Does this have something to do with regionThatFits: being called internally? I don't get it!

但是,我有点认为,如果我可以弄清楚那部分,那么我也许可以弄清楚其他问题.因此,在此我将不再详细介绍.但是,如果您要制作一个快速的应用程序.使用这些功能并运行它,您将看到问题.通常,视图的地图有时是同步的,但是随着您缩放和移动(并在选项卡之间来回切换),跨度开始缩小,每次按下新的选项卡都只会使我越来越远.

However, I kind of think that if I can figure that part out, I might be able to figure out the other issues; so, I won't go into more details here. But, if you make a quick app. with these functions and run it, you'll see the issues. Mainly, the view's maps are occasionally in synch, but as you zoom and move around (and keep switching back and forth between tabs), the span starts to shrink and each new tab press just keeps zooming me farther and farther out.

注意:我只在模拟器(3.0)上运行此程序[我还没有硬件],所以我不知道这是否与它有关.但是,我对此表示怀疑.

Note: I'm only running this on the simulator (3.0) [I don't have the hardware yet], so I don't know if that has anything to do with it. But, I kind of doubt it.

无论如何,感谢您能提供的任何帮助.我真的很感激.

Anyway, thanks for any help you can offer. I really appreciate it.

推荐答案

我正在解决类似的问题,即围绕一组点实现缩放以适合功能.目前,我只能为您提供帮助,是的,regionThatFits:实际上是在内部调用的.我正在调用setRegion,其区域正好适合所有点,因此应该有一个点恰好位于每一边的边缘,以及可见区域内的点. setRegion调用后,mapview实际具有的区域是不同的,但是与调用regionThatFits:与要使用的区域时得到的区域完全相同.这个区域通常比我想要的要大.

I'm working on a similar issue with implementing a zoom-to-fit feature around a set of points. The only help I can give you at the moment is that yes, regionThatFits: is in fact being called internally. I'm calling setRegion with a region that precisely fits all the points, so that there should be a point lying exactly on the edge of every side, along with points inside the viewable area. The region that the mapview actually has after the setRegion call is different, but precisely the same as the region you get if you call regionThatFits: with the region I want to be using. This region is usually somewhat larger than what I want.

我一直在根据regionThatFits仅试图捕获长宽比,并且使长宽比校正变得笨拙的理论进行研究.但是,似乎有两件事表明这并不是唯一的事情:首先,由regionThatFits返回的区域的纵横比与地图视图的纵横比相同.其次,当我使用自己的纵横比校正代码来获得与regionThatFits输出的完全相同的纵横比时,我仍然会从regionThatFits(和mapView)中得到错误的大区域.

I've been working on it under the theory that regionThatFits is merely trying to capture the aspect ratio, and is bungling the aspect ratio correction. However, two things seem to indicate this is not the only thing going on: first, the aspect ratio of the region returned by regionThatFits is not the same as the aspect ratio of the map view. Second, when I use my own aspect correction code to get the exact same aspect ratio that regionThatFits is outputting, I still get the erroneously large region out of regionThatFits (and the mapView).

到目前为止,MapKit确实令人发指.

So far, MapKit is pretty infuriating.

这篇关于MKMapView setRegion:异常行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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