objective-c - MKMapView 中 userLocation 的中心区域

标签 objective-c ios location mkmapview userlocation

我很困惑。我有一个 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 加载时将 map 集中在用户的位置上,并且我还计划在屏幕上实现一个按钮,该按钮将再次手动调用 gotoLocation 以在用户需要时更新用户的位置。

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

最佳答案

来自 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.



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

关于objective-c - MKMapView 中 userLocation 的中心区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5996582/

相关文章:

iphone - 对齐 UITableView 的页脚中的文本

ios - 序列化包含对核心数据中 NSManagedObjects 的引用的多维数组

ios - Xcode 4.2 iOS 空应用程序和 Storyboard

ios - 在 swiftUI 中的 View 数组之间切换

android - LocationClient requestLocationUpdates 与 PendingIntent

ios - 为 Framesetter 提供正确的行间距调整

ios - NSDateFormatter 不适用于 PET 格式

iphone - 如何在 iPhone 上扩展键盘渐变?

java - Android - 只获取一次位置

swift - didUpdateLocations 只能调用一次