MapKit - didUpdateToLocation 已调用但 userLocation 未更新
我有一个 MKMapView 配置如下:
mapView = [[MKMapView alloc] init];
[mapView setMapType:MKMapTypeStandard];
[mapView setShowsUserLocation:YES];
[mapView setDelegate:self];
然后初始化 CLLocationManager 并调用 startUpdatingLocation。
我正在使用 iSimulate 将 GPS 数据从我的手机发送到模拟器,这似乎正在工作因为 CLLocationManager 委托方法是使用我正确的 GPS 坐标调用的。然而,MKMapView 永远不会将蓝点移离库比蒂诺。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"Did Update Location = %f / %f", [newLocation coordinate].latitude, [newLocation coordinate].longitude);
NSLog(@"Current User Location = %f / %f", [[mapView userLocation] coordinate].latitude, [[mapView userLocation] coordinate].longitude);
}
上述方法输出以下内容:
>>> Did Update Location = 40.740100 / -73.989900 # Correct
>>> Current User Location = 37.331693 / -122.030457 # Cupertino... Incorrect
即使我使用以下方法手动更新 userLocation 的坐标:
[[mapView userLocation] setCoordinate:[newLocation coordinate]];
该点仍然位于库比蒂诺上。我错过了什么吗?
I have a MKMapView configured like so:
mapView = [[MKMapView alloc] init];
[mapView setMapType:MKMapTypeStandard];
[mapView setShowsUserLocation:YES];
[mapView setDelegate:self];
I then initialize a CLLocationManager and call startUpdatingLocation.
I am using iSimulate to send the GPS data from my phone, to the simulator, which seems to be working since the CLLocationManager delegate method is invoked with my correct GPS coordinates. However the MKMapView never moves the blue dot away from Cupertino.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"Did Update Location = %f / %f", [newLocation coordinate].latitude, [newLocation coordinate].longitude);
NSLog(@"Current User Location = %f / %f", [[mapView userLocation] coordinate].latitude, [[mapView userLocation] coordinate].longitude);
}
The above method outputs the following:
>>> Did Update Location = 40.740100 / -73.989900 # Correct
>>> Current User Location = 37.331693 / -122.030457 # Cupertino... Incorrect
Even if I manually update the userLocation's coordinate using:
[[mapView userLocation] setCoordinate:[newLocation coordinate]];
The dot still just sits on Cupertino. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CLLocation 管理器的问题是它会缓存旧位置,有时它会返回旧位置。要获取新位置,只需检查 CLLocation 对象的时间戳(如果它早于时间限制),然后忽略此位置
The problem with CLLocation manger is that is caches the old location and some time it returns the old location. To get a new location just check the time stamp of the CLLocation object if it is older than the time limit then ignore this location
你在模拟器上测试这个吗?请注意,在模拟器中,位置点始终保留在库比蒂诺。在设备上尝试一下 - 也许您根本没有错误!
Are you testing this on the simulator? Note that in the simulator, the location dot always remains in Cupertino. Try it on a device - maybe you don't have a bug at all!