MKMapView 中 userLocation 的中心区域
我很困惑。我有一个 MKMapView,在 viewDidLoad 方法中我执行:
- (void)viewDidLoad {
mainDelegate = (PublicArtOmahaAppDelegate*)[[UIApplication sharedApplication]delegate];
XMLController *myXMLController = [[XMLController alloc] init];
[myXMLController parse];
mapView.showsUserLocation = YES;
[self gotoLocation];
// add annotations to map
[self.mapView addAnnotations:mainDelegate.mapAnnotations];
[myXMLController release];
}
[self gotoLocation] 调用:
- (void)gotoLocation
{
MKCoordinateRegion newRegion;
CLLocation *userLocation = mapView.userLocation.location;
float latitude = userLocation.coordinate.latitude;
float longitude = userLocation.coordinate.latitude;
newRegion.center.latitude = latitude;
newRegion.center.longitude = longitude;
[self.mapView setRegion:newRegion animated:YES];
}
所以我认为这应该在加载 mapView 时将地图集中在用户的位置上,并且我还计划在屏幕上实现一个手动按钮再次调用 gotoLocation 以在用户需要时更新用户的位置。
但是...当我在设备上运行该应用程序时,它会加载以非洲西部一片海洋为中心的地图,该地图的纬度和经度显然为 0,0。我觉得奇怪的是,当我放大回我的真实位置时,它正确地将我的位置放置为注释。所以我猜我在 gotoLocation 中设置用户位置的方式有问题?有人注意到我做错了什么吗?
I'm confused. I have an MKMapView, and in the viewDidLoad method I do:
- (void)viewDidLoad {
mainDelegate = (PublicArtOmahaAppDelegate*)[[UIApplication sharedApplication]delegate];
XMLController *myXMLController = [[XMLController alloc] init];
[myXMLController parse];
mapView.showsUserLocation = YES;
[self gotoLocation];
// add annotations to map
[self.mapView addAnnotations:mainDelegate.mapAnnotations];
[myXMLController release];
}
[self gotoLocation] calls:
- (void)gotoLocation
{
MKCoordinateRegion newRegion;
CLLocation *userLocation = mapView.userLocation.location;
float latitude = userLocation.coordinate.latitude;
float longitude = userLocation.coordinate.latitude;
newRegion.center.latitude = latitude;
newRegion.center.longitude = longitude;
[self.mapView setRegion:newRegion animated:YES];
}
So I thought this should center the map on the user's location when the mapView loads and I was also planning on implementing a button on the screen that would manually call gotoLocation again to update the user's location when they want.
But... when I run the app on a device it loads the map centered in a patch of ocean west of Africa which is apparently lat and long 0,0. What I thought was strange was that when I zoomed back to my real location, it had correctly placed my location as an annotation. So I guess there's something wrong with how I'm setting the user location in the gotoLocation? Anyone notice what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
MKUserLocation
文档:MKMapView(或 CLLocationManager)需要几秒钟才能修复用户的位置,并且可能需要几次尝试才能获得相对准确的修复。最好的选择可能是创建一个 CLLocationManager 对象,为其分配一个委托,然后在
locationManager:didUpdateToLocation:fromLocation:
方法触发时缩放地图。From the
MKUserLocation
documentation:It takes a few seconds for the MKMapView (or CLLocationManager) to get a fix on the user's location, and it may take a couple of tries to get a relatively accurate fix. Your best bet is probably to create a
CLLocationManager
object, assign it a delegate, and then zoom the map when thelocationManager:didUpdateToLocation:fromLocation:
method fires.您还应该为该区域设置
span
。将其设置为某个任意值,例如latitudeDelta = 0.01
(与latitudeDelta
相同)。另外,从
-locationManager:didUpdateToLocation:fromLocation:
内部调用gotoLocation
(如果您使用的是位置管理器)。这样,只有当您拥有有效的用户位置时,您才会调用它。You should set the
span
for the region too. Set it to some arbitary value, likelatitudeDelta = 0.01
(and the same forlatitudeDelta
).Also, call
gotoLocation
from inside– locationManager:didUpdateToLocation:fromLocation:
(if you're using a location manager). That way you'll only call it when you have a valid user location.