我的应用上有MKMapView
。这是iOS6。
-(void)viewDidLoad
{
.....
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"Update locations is hit");
NSLog(@"379 the locations count is %d",locations.count);
CLLocation *obj = [locations lastObject];
NSLog(@"the lat is %f", obj.coordinate.latitude);
NSLog(@"the long is %f", obj.coordinate.longitude);
NSLog(@"the horizontal accuracy is %f",obj.horizontalAccuracy);
NSLog(@"the vertical accuracty is %f",obj.verticalAccuracy);
if (obj.coordinate.latitude != 0 && obj.coordinate.longitude != 0)
{
CLLocationCoordinate2D currrentCoordinates ;
currrentCoordinates.latitude = obj.coordinate.latitude;
currrentCoordinates.longitude = obj.coordinate.longitude;
}
....more computation
[locationManager stopUpdatingLocation];
}
首次加载应用时,我的位置显示距离很远。几英里之外。我还有一个重置位置按钮,如果我点击该地图显示正确的位置。这是我在重置位置按钮点击时所拥有的:
- (IBAction)btnResetLocationClick:(UIButton *)sender
{
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
}
那么如何让应用程序在加载时获得正确的当前位置。有没有办法让应用程序告诉地图等待几毫秒,然后更新。还是其他任何想法?请告诉我。如果您需要更多信息,请询问。感谢。
答案 0 :(得分:2)
你能做的是:
didUpdateLocations
中的位置服务,而是didUpdateLocations
感到满意时才会关闭horizontalAccuracy
中的位置服务;和因此,didUpdateLocations
可能看起来像:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
// do whatever you want with the location
// finally turn off location services if we're close enough
//
// I'm using 100m; maybe that's too far for you, but 5m is probably too small
// as you frequently never get that accurate of a location
if (location.horizontalAccuracy > 0 && location.horizontalAccuracy < 100)
[locationManager stopUpdatingLocation];
}
然后在viewDidLoad
中,在经过一段时间后关闭(如果您已经关闭了位置服务,可能需要检查一下您设置的状态变量):
-(void)viewDidLoad
{
.....
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 60.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[locationManager stopUpdatingLocation];
});
}
原始回答:
我看不到您将地图更新到您所在位置的位置。我希望看到类似的东西:
self.mapView.centerCoordinate = location.coordinate;
或者喜欢:
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, 300, 300);
[self.mapView setRegion:region];
我还建议,不要立即关闭位置服务(因为前几个位置经常不准确),请稍等一下,让它磨练你的位置直到horizontalAccuracy
并且verticalAccuracy
属于某个预定限制。查看一些didUpdateLocations
来电的准确性数字,你会看到我的意思。
我原本以为您收到了否定horizontalAccuracy
,此时我建议实施didFailToLocateUserWithError
,因为根据horizontalAccuracy
,“否定值表示该位置的纬度和经度无效。 “希望您收到描述问题所在的错误。即使您目前没有得到负数horizontalAccuracy
,也可能需要实施此方法,以确保正确处理任何错误。
答案 1 :(得分:0)
您无法在iPhone中更准确地使用iPhone中的GPS,但在继续操作之前,您可以检查结果 是否准确。现在你只检查lat而long不是0,但是如果你检查obj
的horizontalAccuracy,那么你就会知道位置信息何时足够好。在发生这种情况之前不要stopUpdatingLoation
。