为什么 MKMapView 把我放到海里?
我有这段代码可以在用户的位置设置地图:
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location = mapView.userLocation.location.coordinate;
MKCoordinateRegion region;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
现在,当我执行 mapView.showsUserLocation=YES
时,它会在正确的位置显示我的位置(在我的设备上,以及模拟器中的苹果总部)然而,上面的代码两次都将我置于非洲附近的大海中!
有什么想法吗?
I have this code to set the map at the user's location:
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location = mapView.userLocation.location.coordinate;
MKCoordinateRegion region;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
Now when I do mapView.showsUserLocation=YES
it shows my location at the right place (on my device, and the apple hq in the simulator) however, both times the code above puts me in the sea somewhere near africa!
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
纬度/经度对 0/0 位于英国格林威治以南的赤道上。该地点位于非洲西海岸附近的几内亚湾。如果您的 pin 放置在此处,则您的 mapView.userLocation 未设置。
可能您的代码在 MKMapKit 有机会获取其位置之前就已运行。您真正想要做的是让您的 viewController 采用
MKMapKitDelegate
协议,将地图视图的.delegate
属性设置为self
,然后实现该方法:当地图套件找到用户时,该方法将被调用,您可以使用传递给该方法的 userLocation 值来设置地图上的区域。不过,在第一次调用之前,MapView 仍在努力定位自身,而您询问其位置还为时过早。
另外,你的regionThatFits最后是一个空操作。该方法不会就地调整区域大小,它实际上返回一个调整大小的区域,供您随意使用。因此,您希望将地图视图的区域设置为该方法返回的区域。像这样:
The latitude/longitude pair 0/0 is on the equator due south of Greenwich, England. That spot is in the Gulf of Guinea, off the West coast of Africa. If that's where your pin is getting placed, then your mapView.userLocation isn't getting set.
Probably your code here is running before MKMapKit has had a chance to get its location. What you really want to do is have your viewController adopt the
MKMapKitDelegate
protocol, set the.delegate
property of your map view toself
, and then implement the method:That method will get called when the map kit locates the user, and you can use the userLocation value you get passed there to set the region on the map. Until that gets called the first time, though, the MapView is still working on locating itself, and your asking about its location is premature.
Also, your regionThatFits there at the end is a no-op. That method doesn't resize the region in place, it actually returns a resized region, for you to use however you will. So you want to set your map view's region to what that method returns. Like this:
我所做的就是在
info.plist
上添加以下条目:将此信息放入此(非常好的)教程中:
https://www.youtube.com/watch?v=Z272SMC9zuQ
What I have done was add the following entry on
info.plist
:Got this info into this (very good) tutorial:
https://www.youtube.com/watch?v=Z272SMC9zuQ
你可以选择这个方法..
}
You can choose this method..
}
我回答这个问题是因为选择的答案并没有真正帮助我,而且我遇到了完全相同的问题。 didUpdateUserLocation 不太实用,因为它会使应用程序在每次位置更改时更新。上述解决方案的另一个问题是,当地图加载时,询问位置还为时过早。那么,如何在地图加载之前询问用户位置,以免缩放时将您带到非洲呢?
您需要使用 viewDidLoad 上的位置管理器来请求位置。然后就可以设置区域了。
SWIFT 3
I am answering this question because the answer picked did not really help me and I was having the exact same issue. didUpdateUserLocation is not very practical because it makes the app update every single time the location changes. The other issue with the above solution is that when the map loads it's still too early to ask for a location. So how do you ask for user location before the the map loads so that the zooming doesn't take you to Africa?
You need to request the location by using the location manager on the viewDidLoad. Then after that you can set the region.
SWIFT 3