如何从“我的”开始来自 MKCoordinateRegion 的坐标?

发布于 2024-11-27 12:53:17 字数 825 浏览 2 评论 0原文

我想知道如何使这段代码工作:当我单击一个单元格时,我调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此刻的回忆 2024-12-04 12:53:17
- (void)viewDidLoad{
[super viewDidLoad];

// define span for map: how much area will be shown
MKCoordinateSpan span;
span.latitudeDelta = 0.002;
span.longitudeDelta = 0.002;

// define starting point for map
CLLocationCoordinate2D start;
start.latitude = 37.786996;
start.longitude = 0.109863;

// create region, consisting of span and location
MKCoordinateRegion region;
region.span = span;
region.center = start;

// move the map to our location
[self.mapView setRegion:region animated:YES];

}

- (void)viewDidLoad{
[super viewDidLoad];

// define span for map: how much area will be shown
MKCoordinateSpan span;
span.latitudeDelta = 0.002;
span.longitudeDelta = 0.002;

// define starting point for map
CLLocationCoordinate2D start;
start.latitude = 37.786996;
start.longitude = 0.109863;

// create region, consisting of span and location
MKCoordinateRegion region;
region.span = span;
region.center = start;

// move the map to our location
[self.mapView setRegion:region animated:YES];

}

优雅的叶子 2024-12-04 12:53:17

由于您看到的是世界地图,因此地图视图的框架必须良好,并且设置区域的代码应该可以工作。

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 the mapView IBOutlet is not hooked up to the map view control in the xib. In IB, right-click on File's Owner and connect the mapView 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.

凤舞天涯 2024-12-04 12:53:17

在设置区域之前需要先设置框架,从外观上看,您正在使用 IB 实例化地图视图,因此您应该将 setRegion 消息放入 viewDidAppear: 并将其放入 DetailViewController 中。

-(void)viewDidAppear {
    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];
}

或者

你可以在设置区域之前执行类似的操作(可能不起作用)

self.mapView.frame = self.view.bounds

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.

-(void)viewDidAppear {
    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];
}

or

you can just do something like this before you set the region (may not work)

self.mapView.frame = self.view.bounds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文