2

startUpdatingLocationアプリの電力を節約するために、アプリがアクティブなstartMonitoringSignificantLocationChangesときとアプリがバックグラウンドにあるときにモードに移行することを組み合わせて使用​​することにしました。基本的に、アプリがバックグラウンドになったときに次のことを行います。

-(void)applicationDidEnterBackground:(UIApplication *)application{
    [myLocationManager startMonitoringSignificantLocationChanges];
}

アプリがフォアグラウンドに戻ったら、次のことを行います。

-(void)applicationDidBecomeActive:(UIApplication *)application{
    //Other irrelevant code
    [myLocationManager stopMonitoringSignificantLocationChanges];
    [myLocationManager startUpdatingLocation];
}

とにかく、これは論理的に思えます。私の質問は、イベントstopUpdatingLocationでメソッドを呼び出す必要がありますか? applicationDidEnterBackgroundそのようです:

-(void)applicationDidEnterBackground:(UIApplication *)application{
    [myLocationManager stopUpdatingLocation];
    [myLocationManager startMonitoringSignificantLocationChanges];
}

メソッドをどこで呼び出す必要がありますstopUpdatingLocationか?? これを行うべき場所が複数ある場合は教えてください。エラーイベントが発生すると更新が停止するはずだと思いますか?

4

1 に答える 1

2

あなたがしていることに何も問題はありません。位置情報サービスを多用する商用アプリがあり、パフォーマンスを向上させ、バッテリーの使用量を最小限に抑えるために書き直しています。

私のリリースされたバージョンは主に (バックグラウンドとフォアグラウンドで) sigLocationChanges を使用しますが、sigLocationChanges が提供する場所の品質に不満がある場合は、startUpdatingLocation の使用に切り替えます。UI はユーザーの場所を大まかに正確に表示する必要があるためです。バッテリーの消耗を最小限に抑えるために、各イベントの直後に stopUpdatingLocation を呼び出します。私の出荷バージョンでは問題なく動作しているように見えますが、私のログ ファイルでは、常に不十分な場所を取得しているように見えるごく一部のユーザーが見つかり、GPS ハードウェアを必要以上に回転させています。

また、プライバシー設定では、アプリに表示される位置アイコンの種類は、完全な GPS 位置情報モードを最後に使用した時期によって決まります。私は、startUpdatingLocation を 1 日に数回短時間しか使用していない場合でも、バッテリーへの影響が大きいことを示す場所アイコンを常に表示します。

私の新しいリリースでは、startUpdatingLocation の使用によるバッテリーの消耗を最小限に抑えるために、その使用をできればゼロに戻しました。アプリがアクティブになると、ロケーション マネージャー cLLocMgr.location から現在のロケーションを直接取得できるようになりました。通常、これは正確な場所であり、UI はすぐに正しく描画 (または更新) できます。また、特定のビューがアクティブになったときに再度チェックして、アプリを開いたままユーザーが移動しているかどうかを確認します。現在、アプリで適切な場所が絶対に必要な特定の状況で電話の場所が悪い場合にのみ、GPS ハードウェアをスピンアップします。その場合、使用を 2 分間に制限し (GPS ハードウェアから最適な位置を取得するには 2 分で十分であると想定しています)、少なくとも 10 分間待ってから再度使用できるようにします。

あなたの質問は、あなたがどれだけ正確である必要があるか、そしてあなたの位置表示がどれほど動的であるかを伝えるのに十分な情報を私に与えません. ただし、非常に正確で動的な表示が必要でない限り、バッテリーを節約するために GPS ハードウェアを起動せずに現在地を使用することを検討する必要があります。

編集:これは、私がジェラルドに使用した実際のコードで、少しクリーンアップされています。触れてから1年が経ちましたので、少し錆びています。何も片付けていないことを願っています。

 // Called at start to ask user to authorize location data access.
- (void) requestIOSLocationMonitoring {
#if TARGET_IPHONE_SIMULATOR
    // If running in siumaltor turn on continuous updating (GPS mode)
    // This is for testing as significant change isn't useful in simulator
    // Set a movement threshold for new events. This is only used by continiuous mode, not sig change events
    // Keep it as low as possible,but not so low as to generate spurious movements.
    cLLocMgr.distanceFilter = 30;

    // Use continuous location events in simulator.
    // desired accuracy only works in continuious (high power) mode.
    cLLocMgr.desiredAccuracy = kCLLocationAccuracyBest;
    [cLLocMgr startUpdatingLocation];
#else
    // If not in simulator app's default is significant change monitoring
    [cLLocMgr startMonitoringSignificantLocationChanges];
#endif //TARGET_IPHONE_SIMULATOR
}

// Toggle back and forth between continius updates (GPS on) and 
// significant change monitoring
- (void)    setGPSMode: (bool) useGPSMode  {
    // Keep track of time since we last changed GPS mode
    NSTimeInterval secsInThisMode = [[NSDate date] timeIntervalSinceDate: lastModeChange];

    // inGPSMode is an object instance variable app uses to track mode it is in.
    if (inGPSMode != useGPSMode) {
        lastModeChange = [NSDate date];
        if (!useGPSMode) {
            // Tell app to operate as if continuous updating is off 
            inGPSMode = false;
#if TARGET_IPHONE_SIMULATOR
            // But keep using continuous location events in simulator for testing.
            cLLocMgr.distanceFilter = 30;
#else
            // Turn off continious updates for app on actual devices
            [cLLocMgr stopUpdatingLocation];
#endif
        } else if (secsInThisMode > cMinGPSModeBreak) {
            // Only turn on continuous updating again if it's been off long enough
            // Prevents GPS mode from running continiously and killing battery life
            inGPSMode = true;
            cLLocMgr.desiredAccuracy = kCLLocationAccuracyBest;
            cLLocMgr.distanceFilter = kCLDistanceFilterNone;
            [cLLocMgr startUpdatingLocation];
        }
    }
}
于 2013-02-15T19:04:34.807 に答える