gpt4 book ai didi

iphone - MapKit - didUpdateToLocation 已调用但 userLocation 未更新

转载 作者:行者123 更新时间:2023-12-03 21:07:50 25 4
gpt4 key购买 nike

我有一个 MKMapView 配置如下:

mapView = [[MKMapView alloc] init];     
[mapView setMapType:MKMapTypeStandard];
[mapView setShowsUserLocation:YES];
[mapView setDelegate:self];

然后我初始化 CLLocationManager 并调用 startUpdatingLocation。

我正在使用iSimulate将 GPS 数据从我的手机发送到模拟器,这似乎可以正常工作,因为 CLLocationManager 委托(delegate)方法是使用我正确的 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]];

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

最佳答案

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

关于iphone - MapKit - didUpdateToLocation 已调用但 userLocation 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com