MKMapView 中 userLocation 的中心区域

发布于 2024-11-06 18:33:15 字数 1152 浏览 1 评论 0原文

我很困惑。我有一个 MKMapView,在 viewDidLoad 方法中我执行:

- (void)viewDidLoad {
    mainDelegate = (PublicArtOmahaAppDelegate*)[[UIApplication sharedApplication]delegate]; 

    XMLController  *myXMLController = [[XMLController alloc] init]; 
    [myXMLController parse];
    mapView.showsUserLocation = YES;
    [self gotoLocation];

    // add annotations to map
    [self.mapView addAnnotations:mainDelegate.mapAnnotations];
    [myXMLController release];
}

[self gotoLocation] 调用:

- (void)gotoLocation
{
    MKCoordinateRegion newRegion;

    CLLocation *userLocation = mapView.userLocation.location;
    float latitude = userLocation.coordinate.latitude;
    float longitude = userLocation.coordinate.latitude;
    newRegion.center.latitude = latitude;
    newRegion.center.longitude = longitude;

    [self.mapView setRegion:newRegion animated:YES];
}

所以我认为这应该在加载 mapView 时将地图集中在用户的位置上,并且我还计划在屏幕上实现一个手动按钮再次调用 gotoLocation 以在用户需要时更新用户的位置。

但是...当我在设备上运行该应用程序时,它会加载以非洲西部一片海洋为中心的地图,该地图的纬度和经度显然为 0,0。我觉得奇怪的是,当我放大回我的真实位置时,它正确地将我的位置放置为注释。所以我猜我在 gotoLocation 中设置用户位置的方式有问题?有人注意到我做错了什么吗?

I'm confused. I have an MKMapView, and in the viewDidLoad method I do:

- (void)viewDidLoad {
    mainDelegate = (PublicArtOmahaAppDelegate*)[[UIApplication sharedApplication]delegate]; 

    XMLController  *myXMLController = [[XMLController alloc] init]; 
    [myXMLController parse];
    mapView.showsUserLocation = YES;
    [self gotoLocation];

    // add annotations to map
    [self.mapView addAnnotations:mainDelegate.mapAnnotations];
    [myXMLController release];
}

[self gotoLocation] calls:

- (void)gotoLocation
{
    MKCoordinateRegion newRegion;

    CLLocation *userLocation = mapView.userLocation.location;
    float latitude = userLocation.coordinate.latitude;
    float longitude = userLocation.coordinate.latitude;
    newRegion.center.latitude = latitude;
    newRegion.center.longitude = longitude;

    [self.mapView setRegion:newRegion animated:YES];
}

So I thought this should center the map on the user's location when the mapView loads and I was also planning on implementing a button on the screen that would manually call gotoLocation again to update the user's location when they want.

But... when I run the app on a device it loads the map centered in a patch of ocean west of Africa which is apparently lat and long 0,0. What I thought was strange was that when I zoomed back to my real location, it had correctly placed my location as an annotation. So I guess there's something wrong with how I'm setting the user location in the gotoLocation? Anyone notice what I'm doing wrong?

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

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

发布评论

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

评论(2

傲性难收 2024-11-13 18:33:15

来自 MKUserLocation文档

位置

设备的当前位置。 (只读)

@property(只读,非原子)CLLocation *位置

讨论

如果地图视图当前未显示用户位置或用户位置尚未确定,则此属性包含 nil。

MKMapView(或 CLLocationManager)需要几秒钟才能修复用户的位置,并且可能需要几次尝试才能获得相对准确的修复。最好的选择可能是创建一个 CLLocationManager 对象,为其分配一个委托,然后在 locationManager:didUpdateToLocation:fromLocation: 方法触发时缩放地图。

From the MKUserLocation documentation:

location

The current location of the device. (read-only)

@property (readonly, nonatomic) CLLocation *location

Discussion

This property contains nil if the map view is not currently showing the user location or if the user’s location has not yet been determined.

It takes a few seconds for the MKMapView (or CLLocationManager) to get a fix on the user's location, and it may take a couple of tries to get a relatively accurate fix. Your best bet is probably to create a CLLocationManager object, assign it a delegate, and then zoom the map when the locationManager:didUpdateToLocation:fromLocation: method fires.

请恋爱 2024-11-13 18:33:15

您还应该为该区域设置 span。将其设置为某个任意值,例如 latitudeDelta = 0.01(与 latitudeDelta 相同)。

另外,从 -locationManager:didUpdateToLocation:fromLocation: 内部调用 gotoLocation(如果您使用的是位置管理器)。这样,只有当您拥有有效的用户位置时,您才会调用它。

You should set the span for the region too. Set it to some arbitary value, like latitudeDelta = 0.01 (and the same for latitudeDelta).

Also, call gotoLocation from inside – locationManager:didUpdateToLocation:fromLocation: (if you're using a location manager). That way you'll only call it when you have a valid user location.

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