如何从“我的”开始来自 MKCoordinateRegion 的坐标?
我想知道如何使这段代码工作:当我单击一个单元格时,我调用 DetailViewController,它是带有旧金山坐标的地图。但我的地图只显示“世界”地图,并且不去我设置的区域,你知道我如何从“我的”坐标开始吗?
这是我的代码:
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize mapView;
- (void) gotoLocation {
NSLog(@"allo");
//start off in San Francisco
MKCoordinateRegion region;
region.center.latitude = 37.786996;
region.center.longitude = -122.440100;
region.span.latitudeDelta = 0.112872;
region.span.longitudeDelta = 0.109863;
[self.mapView setRegion:region animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self gotoLocation];
}
- (void)dealloc {
[super dealloc];
[mapView release];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.mapView = nil;
}
@end
谢谢
保罗
i'm wondering how i can make this code work : when i click on a cell, i call the DetailViewController, which is a map with the coordinates of San Francisco. But my map shows only the "world" map, and don't go to the region i set up, do you know how i could start off with "my" coordinates?
Here's my code :
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize mapView;
- (void) gotoLocation {
NSLog(@"allo");
//start off in San Francisco
MKCoordinateRegion region;
region.center.latitude = 37.786996;
region.center.longitude = -122.440100;
region.span.latitudeDelta = 0.112872;
region.span.longitudeDelta = 0.109863;
[self.mapView setRegion:region animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self gotoLocation];
}
- (void)dealloc {
[super dealloc];
[mapView release];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.mapView = nil;
}
@end
Thanks
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
}
}
由于您看到的是世界地图,因此地图视图的框架必须良好,并且设置区域的代码应该可以工作。
setRegion
调用不起作用的最可能原因是mapView
IBOutlet 未连接到 xib 中的地图视图控件。在 IB 中,右键单击“文件所有者”并将mapView
出口连接到地图视图控件。您可能还希望将地图视图控件的
delegate
出口连接到文件的所有者,以便调用您(稍后)实现的任何委托方法。另外(与您的问题无关),在
dealloc
中,应最后调用[super dealloc]
。有关解释,请参阅此答案。Since you see the world map, the map view's frame must be fine and that code to set the region should work.
The most likely reason the
setRegion
call doesn't work is that themapView
IBOutlet is not hooked up to the map view control in the xib. In IB, right-click on File's Owner and connect themapView
outlet to the map view control.You may also want to connect the map view control's
delegate
outlet to File's Owner as well so any delegate methods you implement (later) will get called.Separately (and unrelated to your problem), in
dealloc
,[super dealloc]
should be called last. See this answer for an explanation.在设置区域之前需要先设置框架,从外观上看,您正在使用 IB 实例化地图视图,因此您应该将 setRegion 消息放入 viewDidAppear: 并将其放入 DetailViewController 中。
或者
你可以在设置区域之前执行类似的操作(可能不起作用)
Your frame needs to be set before you can set the region, by the looks of this you are using IB to instantiate the mapview, so you should probably put the setRegion message into the viewDidAppear: and put this into your DetailViewController.
or
you can just do something like this before you set the region (may not work)