为什么MKMapView将我带入大海? [英] Why is MKMapView putting me in the sea?

查看:94
本文介绍了为什么MKMapView将我带入大海?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可将地图设置在用户的位置:

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];

现在,当我执行mapView.showsUserLocation=YES时,它会在正确的位置显示我的位置(在我的设备上,并且在模拟器中是Apple hq),但是上面的代码两次都将我放置在非洲附近的某个地方!

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!

有什么想法吗?

推荐答案

0/0对的纬度/经度位于英格兰格林威治以南的赤道上.该地点位于非洲西海岸的几内亚湾.如果那是您放置图钉的位置,则说明您的mapView.userLocation没有设置.

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.

您的代码可能在MKMapKit有机会获得其位置之前就已经运行了.您真正想要做的是让viewController采用MKMapKitDelegate协议,将地图视图的.delegate属性设置为self,然后实现方法:

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 to self, and then implement the method:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

当地图工具包找到用户时,该方法将被调用,您可以使用在那里传递的userLocation值来设置地图上的区域.不过,在首次调用之前,MapView仍在自行定位,而您询问其位置为时过早.

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:

[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];

这篇关于为什么MKMapView将我带入大海?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆