更新中心点和MKMapView 的缩放 (iPhone)
我一直在尝试将地图视图集中在用户位置上。当注册位置变化时,这也应该更新。我遇到的问题是加载应用程序时我无法获取当前的纬度和经度来应用居中和定位。缩放。
这些是我到目前为止所拥有的关键代码...
- (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];
}
我尝试查看其他几个被问及的类似问题。之前回答过,但我似乎仍然找不到我想要的解决方案。
对此的任何帮助将不胜感激;我花了很多时间在互联网上寻找帮助和解决方案。
附注“地图”是地图视图。
I have been trying to centre the map view on the users location. This should also update as and when a change in location is registered. The issue I'm having is upon loading the app I can't get hold of the current latitude and longitude to apply the centring & zooming.
These are the key bits of code I have so far...
- (void)viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[self updateMapZoomLocation:locationManager.location];
[super viewDidLoad];
}
The idea being that it should create an instance of the location manager, then start updating the location. Finally it should run the function I wrote to update the map view accordingly...
- (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];
}
However this doesn't seem to happen. The app builds and runs ok, but all that gets displayed is a black screen - it's as if the coordinates don't exist?!
I also have a delegate method that deals with any updates by calling the function above...
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self updateMapZoomLocation:newLocation];
}
I have tried looking at several of the other similar questions that have been asked & answered previously however I still can't seem to find the solution I'm after.
Any help on this would be really appreciated; I've spent hours and hours trawling the internet in search of help and solutions.
ps. 'map' is the mapView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要使用 MKMapViewDelegate 回调,以便只有在实际知道位置后才进行放大:
下面是一些处理该问题的快速代码:
You want to use the MKMapViewDelegate callback so you only zoom in once the location is actually known:
Here's some swift code to handle that:
您不应在 viewDidLoad 函数期间调用 updateMapZoomLocation,因为位置管理器尚未到达某个位置。如果/当它完成时,它将在准备好时调用委托函数。在那之前你的地图将不知道以哪里为中心。您可以尝试尽可能缩小,或者记住应用程序关闭之前它最后一次查看的位置。
You shouldn't call updateMapZoomLocation during your viewDidLoad function because the location manager has not yet go a location. If/when it does it'll call the delegate function when it's ready. Until then your map won't know where to center. You could try zooming as far out as it goes, or remembering where it was last looking before the app was shutdown.