1

我的应用上有一个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];
}

那么如何让应用程序在加载时获得正确的当前位置。有没有办法让应用程序告诉地图等待几毫秒然后更新。还是有其他想法?请告诉我。如果您需要更多信息,请询问。谢谢。

4

2 回答 2

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,您也可能希望实现此方法,以确保您正确处理任何错误。

于 2013-01-21T21:17:50.297 回答
0

您无法在您的应用程序中使 iPhone 中的 GPS 更准确,但您可以在继续之前检查结果是否准确。现在你只检查 lat 和 long 不是 0,但如果你检查obj'horizo​​ntalAccuracy 那么你就会知道位置信息什么时候足够好。在发生这种情况之前不stopUpdatingLoation要这样做。

于 2013-01-21T21:06:31.847 回答