0

我的应用程序在两台设备上,一个在支持 iOS7 的设备上,另一个在支持 iOS8 的设备上。我的 MKMapView 不是以 iOS8 设备上的用户位置为中心,而是在 iOS7 上。

这是我的代码,我已请求在另一个屏幕上使用用户位置。

-(void)viewDidLoad{

[LocationManager sharedInstance];
CLLocation * location = [[LocationManager sharedInstance] getCurrentLocation];
NSLog(@"Location %f %f",location.coordinate.latitude,location.coordinate.longitude);
MKCoordinateRegion mapRegion;
mapRegion.center = location.coordinate;
mapRegion.span.latitudeDelta = 0.01;
mapRegion.span.longitudeDelta = 0.01;
 NSLog(@"Map Region Lat %f",mapRegion.center.latitude);
NSLog(@"Map Region Long %f ",mapRegion.center.longitude);

self.getDirectionsMap.delegate = self;
[self.getDirectionsMap setRegion:mapRegion animated: NO];
self.getDirectionsMap.showsUserLocation=YES;
self.getDirectionsMap.showsPointsOfInterest=YES;




}

我的位置管理器对象 .m

+ (LocationManager *)sharedInstance {
if (nil == kLocationManager) {

    kLocationManager = [[LocationManager alloc] init];
}

return kLocationManager;
}

-(CLLocation *)getCurrentLocation{
CLLocation *loc = [_locationManager location];

return loc;
}

- (id)init {
    self = [super init];
    if (!self) return nil;

    /* setup location manager */
    _locationManager = [[CLLocationManager alloc] init];
    [_locationManager setDelegate:self];

    //iOS8 check
    if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [_locationManager requestWhenInUseAuthorization];
    }
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [_locationManager setDistanceFilter:DISTANCE_FILTER];


    return self;
 }

我的地图成功显示了用户的位置,但地图没有居中。我的位置也使用正确的坐标正确记录,这也与记录的地图区域纬度和经度相匹配。

4

1 回答 1

1

您的 viewDidLoad 方法尚未检查是否确实存在要使用的位置。如果你使用了 CLLocationManager,你可以设置它,然后当它有一个实际位置时它会调用委托的方法。我怀疑 iOS 设备需要更长的时间才能获得 GPS 读数,并且它会nil在 viewDidLoad 中为您提供。

于 2014-09-20T03:31:29.917 回答