【发布时间】:2011-11-25 12:07:05
【问题描述】:
我一直在尝试将地图视图集中在用户位置上。这也应该在注册位置更改时更新。我遇到的问题是在加载应用程序时我无法掌握当前的纬度和经度来应用居中和缩放。
这些是我目前掌握的关键代码...
- (void)viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[self updateMapZoomLocation:locationManager.location];
[super viewDidLoad];
}
这个想法是它应该创建一个位置管理器的实例,然后开始更新位置。最后它应该运行我编写的函数来相应地更新地图视图......
- (void)updateMapZoomLocation:(CLLocation *)newLocation
{
MKCoordinateRegion region;
region.center.latitude = newLocation.coordinate.latitude;
region.center.longitude = newLocation.coordinate.longitude;
region.span.latitudeDelta = 0.1;
region.span.longitudeDelta = 0.1;
[map setRegion:region animated:YES];
}
然而这似乎并没有发生。该应用程序构建并运行正常,但显示的只是黑屏 - 好像坐标不存在?!
我还有一个委托方法,通过调用上面的函数来处理任何更新......
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self updateMapZoomLocation:newLocation];
}
我已经尝试查看之前已经提出和回答的其他几个类似问题,但我似乎仍然找不到我想要的解决方案。
对此的任何帮助将不胜感激;我花了好几个小时在互联网上寻找帮助和解决方案。
ps。 'map' 是 mapView。
【问题讨论】:
-
locationManager:didUpdateToLocation:fromLocation被调用了吗?或者它被调用但你没有得到一个有效的位置?您应该丢弃 newLocation 具有负水平精度(表示它无效)的任何位置更新。将 NSLog 添加到locationManager:didUpdateToLocation:fromLocation以显示水平精度和纬度/经度,并将该信息包含在您的问题中。
标签: objective-c ios4 mkmapview core-location latitude-longitude