MapKit - didUpdateToLocation 已调用但 userLocation 未更新

发布于 2024-10-25 14:37:04 字数 1236 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我喜欢麦丽素 2024-11-01 14:37:04

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
    }
}

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

-(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
    }
}
美羊羊 2024-11-01 14:37:04

该点仍然位于库比蒂诺。我错过了什么吗?

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

The dot still just sits on Cupertino. Am I missing something?

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文