2011-03-24 73 views
2

我有一个的MKMapView配置,像这样:MapKit - didUpdateToLocation叫,但用户位置不更新

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 

即使我手动更新用户位置的坐标使用:

[[mapView userLocation] setCoordinate:[newLocation coordinate]]; 

的点仍然只是坐在库珀蒂诺。我错过了什么吗?

回答

2

CLLocation管理器的问题在于缓存旧位置,并在某个时间返回旧位置。为了得到一个新的位置,就检查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 
    } 
} 
+0

它永远不会进入循环,你永远不会得到新的位置只是检查它... – shankar 2013-01-19 07:13:17

0

的点仍然只是坐在库比蒂诺。我错过了什么吗?

你在模拟器上测试这个吗?请注意,在模拟器中,位置点始终保留在库比蒂诺。在设备上试用 - 也许你根本没有错误!

+0

如果你看了这个问题,你会看到我使用iSimulate从我的手机发送GPS数据到SIM卡。长话短说,这似乎是iSimulate软件+ SDK的一个缺陷。 – nrj 2011-12-06 21:12:43