【发布时间】:2013-12-05 10:03:18
【问题描述】:
我像这样初始化我的 mapviewControler:
- (void)viewDidLoad
{
[super viewDidLoad];
self.firstShow=TRUE;
self.mapView.delegate=self;
self.mapView.showsUserLocation=YES;
self.cllmng=[[CLLocationManager alloc]init];
self.cllmng.delegate=self;
self.cllmng.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
self.cllmng.distanceFilter=50;
[self.cllmng startUpdatingLocation];
}
我让 mapviewControler 实现 CLLocationManagerDelegate,并在我的代码中实现 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations。
我遇到的问题是我想以适当的比例以当前用户位置为中心来初始化地图视图。我打算这样做
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation * currentLoci=[locations lastObject];
MKCoordinateRegion r;
MKCoordinateSpan span;
span.latitudeDelta = 1;
span.longitudeDelta = 1;
r.span = span;
CLLocationCoordinate2D c;
c.longitude=currentLoci.coordinate.longitude;
c.latitude=currentLoci.coordinate.latitude;
r.center = c;
[self.mapView setRegion:r animated:YES];
}
但有时,在 -(void) viewDidload 中调用 [self.cllmng startUpdatingLocation] 后,我无法立即接到 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 的电话。因此,地图视图刚开始显示用户位置,但显示了整个澳大利亚。即使didUpdateLocations 未被触发,我如何在地图视图的初始化中设置比例/跨度? THX!
【问题讨论】:
-
为什么要使用 CLLocationManager 来获取用户位置?由于您在地图视图上将 showsUserLocation 设置为 YES,因此它将调用自己的委托方法 didUpdateUserLocation,该方法应与蓝点同步。
-
@安娜。只是为了获取当前位置并使用它在 didUpdateLocation 方法中创建适当的区域。克雷格给了我想要的。还是谢谢你。