我有一个像这样配置的MKMapView:
mapView = [[MKMapView alloc] init];
[mapView setMapType:MKMapTypeStandard];
[mapView setShowsUserLocation:YES];
[mapView setDelegate:self];
然后我初始化一个CLLocationManager并调用startUpdatingLocation。
我正在使用iSimulate将GPS数据从我的手机发送到模拟器,这似乎正在工作,因为使用我正确的GPS坐标调用了CLLocationManager委托方法。然而,MKMapView永远不会将蓝点从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);
}
上述方法输出以下内容:
>>> Did Update Location = 40.740100 / -73.989900 # Correct
>>> Current User Location = 37.331693 / -122.030457 # Cupertino... Incorrect
即使我使用以下方法手动更新userLocation的坐标:
[[mapView userLocation] setCoordinate:[newLocation coordinate]];
点仍然只是坐在库比蒂诺。我错过了什么吗?
答案 0 :(得分:2)
CLLocation manger的问题在于缓存旧位置,有时则返回旧位置。要获取新位置,只需检查CLLocation对象的时间戳(如果它早于时间限制),然后忽略此位置
-(void) locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*) oldLocation
{
NSDate* time = newLocation.timestamp;
NSTimeInterval timePeriod = [time timeIntervalSinceNow];
if(timePeriod < 2.0 ) { //usually it take less than 0.5 sec to get a new location but you can use any value greater than 0.5 but i recommend 1.0 or 2.0
[manager stopUpdatingLocation];
// process the location
} else {
// skip the location
}
}
答案 1 :(得分:0)
点仍然只是坐在库比蒂诺。我错过了什么吗?
你在模拟器上测试这个吗?请注意,在模拟器中,位置点始终保留在Cupertino中。在设备上试试吧 - 也许你根本没有错误!