我的应用程序在两台设备上,一个在支持 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;
}
我的地图成功显示了用户的位置,但地图没有居中。我的位置也使用正确的坐标正确记录,这也与记录的地图区域纬度和经度相匹配。