MapView 未缩放至用户位置

发布于 2024-10-20 10:01:03 字数 839 浏览 4 评论 0原文

我的地图视图未缩放至我的用户位置。请指出错误:

- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] init];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;

[self performSelector:@selector(startupZoom) withObject:nil afterDelay:1.25];
}
- (void)startupZoom {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=mapView.userLocation.coordinate.latitude;
location.longitude=mapView.userLocation.coordinate.longitude;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
NSLog(@"%f, %f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);
}

My mapview is not zooming to my user location. Please can you point out the error:

- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] init];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;

[self performSelector:@selector(startupZoom) withObject:nil afterDelay:1.25];
}
- (void)startupZoom {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=mapView.userLocation.coordinate.latitude;
location.longitude=mapView.userLocation.coordinate.longitude;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
NSLog(@"%f, %f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

轮廓§ 2024-10-27 10:01:03

正在创建地图视图,但其框架尚未设置,并且未将其添加为视图控制器视图的子视图。

将 alloc+init 行更改为(根据需要更改尺寸):

mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];

然后在 PerformSelector 行之前添加以下内容:

[self.view addSubview:mapView];

但请注意,最好使用 MKMapView 的 didUpdateUserLocation 委托方法来缩放到用户的位置假设用户位置在某个任意的固定间隔(例如 1.25 秒)后可用。

例如,删除performSelector行并实现didUpdateUserLocation:

- (void)mapView:(MKMapView *)mapView 
            didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [self startupZoom];
}

使用didUpdateUserLocation方法唯一可能的问题是每次用户位置更改时都会调用它,因此如果您只想在启动时缩放一次,则需要添加一个BOOL标志ivar 并相应地检查/设置它。

The mapView is being created but its frame is not set and it's not being added as a subview of the view controller's view.

Change the alloc+init line to (change the dimensions as needed):

mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];

Then before the performSelector line, add this:

[self.view addSubview:mapView];

Note however that it's better to use the MKMapView's didUpdateUserLocation delegate method to zoom to the user's location instead of assuming the user location will be available after some arbitrary, fixed interval like 1.25 seconds.

For example, remove the performSelector line and implement didUpdateUserLocation:

- (void)mapView:(MKMapView *)mapView 
            didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [self startupZoom];
}

Only possible problem using the didUpdateUserLocation method is that it will be called every time the user's location changes so if you only want to zoom once on startup, you'll need to add a BOOL flag ivar and check/set it accordingly.

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