将 MKMapView 放在 userLocation 上(最初) - 仅适用于 iPhone 4?

发布于 2024-10-18 22:49:26 字数 1118 浏览 1 评论 0 原文

我实现了以下 MKMapView 方法,该方法在添加注释后运行。我在 Interface Builder 中将 MKMapView 地图(parishMap)设置为“显示用户位置”,加载地图视图后,蓝点总是在一秒左右的时间内出现在地图视图上。

以下是我的地图视图控制器实现中的代码:

// Center map on user location (initially)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for(MKAnnotationView *annotationView in views) {
        if(annotationView.annotation == parishMap.userLocation) {
            MKCoordinateRegion region;
            MKCoordinateSpan span;

            span.latitudeDelta=0.03;
            span.longitudeDelta=0.03;

            CLLocationCoordinate2D location=parishMap.userLocation.coordinate;

            location = parishMap.userLocation.location.coordinate;

            region.span=span;
            region.center=location;

            [parishMap setRegion:region animated:TRUE];
            [parishMap regionThatFits:region];
        }
    }
}

当我在 iPhone 4 上打开地图视图时,地图视图总是(并且几乎立即)移动到 userLocation 点的中心。但是,当我在 iPhone 3Gs 或 iPod Touch (2010) 上打开地图视图时,它并不以用户位置为中心,而只是停留在我为地图视图设置的默认缩放/区域。

关于为什么此代码不适用于非 iPhone 4 设备(它们都运行 4.2.x 最新 iOS)的任何想法?

I implemented the following MKMapView method, which runs after annotations are added. I have my MKMapView map (parishMap) set to "Shows user location" in Interface Builder, and upon loading the mapview, the blue dot always appears within a second or so on the mapview.

Here's the code I have in my Map View controller implementation:

// Center map on user location (initially)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for(MKAnnotationView *annotationView in views) {
        if(annotationView.annotation == parishMap.userLocation) {
            MKCoordinateRegion region;
            MKCoordinateSpan span;

            span.latitudeDelta=0.03;
            span.longitudeDelta=0.03;

            CLLocationCoordinate2D location=parishMap.userLocation.coordinate;

            location = parishMap.userLocation.location.coordinate;

            region.span=span;
            region.center=location;

            [parishMap setRegion:region animated:TRUE];
            [parishMap regionThatFits:region];
        }
    }
}

When I open the mapview on my iPhone 4, the mapview always (and almost immediately) moves to center on the userLocation dot. However, when I open the mapview on an iPhone 3Gs or iPod Touch (2010), it doesn't center on the userLocation, but just stays at the default zoom/region I set for the map view.

Any ideas as to why this code won't work for non-iPhone 4 devices (they're all running 4.2.x latest iOS)?

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

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

发布评论

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

评论(2

不知所踪 2024-10-25 22:49:26

这可能与 GPS 在这些不同设备上锁定位置所需的时间有关。

我建议使用 CLLocationManager 及其委托方法来使地图居中。

您可以通过创建 CLLocationManager 的实例来开始更新位置,将其委托属性设置为 self,然后实现:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

您可以根据最新的位置更新将地图相应地居中。

This is probably an issue with the amount of time it takes for the GPS to lock a position on these different devices.

I'd recommend using CLLocationManager and its delegate methods to center the map.

You can start updating location by creating an instance of CLLocationManager, setting its delegate property to self and then implementing:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

Where you can center your map accordingly based on the latest location update.

轻拂→两袖风尘 2024-10-25 22:49:26

您是否尝试过使用此代码将地图居中:

[mapView setCenterCoordinate:location animated:YES];

我不确定为什么它适用于 iPhone 4 而不是其他手机,但这是我用来调整区域的代码:

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.03, 0.03);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];

    [mapView setRegion:adjustedRegion animated:YES];

尝试一下这种方式,看看它是如何工作的。您执行 RegionThatFits 方法的方式实际上不应该是该方法的使用方式。您可以尝试注释掉代码的最后一行,它应该没有什么区别。无论如何,请按照我刚刚提供的方法尝试一下,如果有任何问题请告诉我。

have you tried using this code to center the map:

[mapView setCenterCoordinate:location animated:YES];

I'm not sure why it would work for iPhone 4 and not the others, but this is the code I use to adjust the region:

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.03, 0.03);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];

    [mapView setRegion:adjustedRegion animated:YES];

Try it that way and see how that works. The way you're doing the regionThatFits method, that's actually not supposed to be how the method is used. You can try commenting out that last line of your code, it should make no difference. Anyway, try it with the method I just gave and let me know if you have any questions.

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