1

我很困惑。我有一个 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 中设置用户位置的方式有问题吗?有人注意到我做错了什么吗?

4

2 回答 2

3

MKUserLocation文档中:

地点

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

@property (readonly, nonatomic) CLLocation *location

讨论

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

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

于 2011-05-13T19:41:05.870 回答
0

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.

于 2011-05-13T20:00:49.460 回答