0

I need to know if I am near to a saved Geo-location in my app within a range starting at 10 Meters to 500 Meters in background. I have used startMonitoringForSignificantLocationChange but I am not getting accurate results. Instead using startupdatinglocation is giving results as accurate as 7 meters. I am planning to add a NSTimer to call startupdatinglocation every 1 minute.

Is there any other solutions?

4

2 回答 2

2

首先,请不要每分钟都调用 startUpdatingLocation。您将杀死用户设备上的电池寿命。定位服务使用大量电池电量来运行。此外,一旦你打电话startUpdatingLocation,它会一直运行,直到你打电话stopUpdatingLocation

您可以设置一些属性CLLocationManager以根据需要对其进行配置。喜欢...

@property(assign, nonatomic) CLLocationDistance distanceFilter
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy

distanceFilter描述:在生成更新事件之前,设备必须水平移动的最小距离(以米为单位)。

desiredAccuracy可以是以下之一:

kCLLocationAccuracyBestForNavigation
kCLLocationAccuracyBest;
kCLLocationAccuracyNearestTenMeters;
kCLLocationAccuracyHundredMeters;
kCLLocationAccuracyKilometer;
kCLLocationAccuracyThreeKilometers;

根据CLLocationManager对象的配置,设备将使用某些地理位置硬件而不是其他硬件。有时简单地使用蜂窝塔三角测量就足够了,并且是获得半准确位置的最快方法。其他时候它可能需要使用 GPS,这会很快耗尽电池寿命并且需要更长的时间才能找到位置,但在大多数情况下更准确。

我建议您启动一个 NSTimer,但将其用于与您计划相反的用途。如果无法获得足够准确的位置,请stopUpdatingLocation在合理的时间后使用它。CLLocationManager您不想让定位服务永远运行。将此计时器设置为 30 秒将确保它在超时后关闭。

此外,您将需要实现 CLLocationManagerDelegate 方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在这种方法中,您需要检查timestamp该位置以确保它是最近的。您还需要检查horizontalAccuracy位置以确保它在所需的精度范围内。这是如何执行此操作的示例...

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Ensure we get a recent and accurate location
    NSDate * locationTimestamp = [newLocation timestamp];
    NSTimeInterval timeDifference = [locationTimestamp timeIntervalSinceNow];
    double thirtyMins = (30 * 60 * -1);
    BOOL locationTimestampIsCurrent = (timeDifference > thirtyMins);
    BOOL isAccurateLocation = (newLocation.horizontalAccuracy <= 3000.00);

    if (locationTimestampIsCurrent && isAccurateLocation) {
        NSLog(@"Shutting down location services, we have a good locaton.");
        [self stopListeningForLocation];
    } else {
        // Do nothing, let this method be called again with a new "newLocation" to check its validity.
    }
}

从 iOS 6 开始,不推荐使用上述方法,而是使用以下委托方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

要获取最新位置,只需获取位置数组的最后一个对象,如下所示:

  CLLocation *location = [locations lastObject];
于 2013-05-21T02:12:41.483 回答
0

你真的是从头开始回答这些问题。我建议您关注 Rob 和 Abhijit 链接。你也可以从我在 Github 上的代码中受益:

TTLLocationHandler Github

于 2013-05-21T01:24:32.750 回答