2013-03-28 8 views
6

ネットワークとGPSプロバイダの両方から位置の変更を受け取る必要があります。Android LocationManager基準

GPSプロバイダが利用できない場合や、場所が不明な場合(衛星衛星の可視性が悪い場合)、GPSプロバイダ以外のネットワークプロバイダからの位置情報を受信します。

私の必要に応じて基準を使用してプロバイダを選択することはできますか?

+0

設定に応じて利用可能な最善のプロバイダを見つけるためにCriteriaクラスを使用している場合は、GPSプロバイダ、ネットワークプロバイダ、またはパッシブの誰でも結果になります。 Criteriaクラスを使用しないと、プロバイダへの位置付けを取得するためのrequestLocationUpdatesを使用します。 –

+0

LocationManagerはネットワークとGPSの両方を1つのコールバックで通知しません。別のコールバックで2つのrequestLocation()を使用する必要がありますか? – Nik

+0

更新プログラムを取得するためにロケーションマネージャを登録するには、アップデートを取得するプロバイダを指定する必要があります。 'mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER、10 * 1000、50、this); ' –

答えて

9

実際Android Developers - Making Your App Location Awareがあなたのニーズを満たすのに最適なサンプルコードがあります。指定された期間内の精度を決定することにより、(

... 
} else if (mUseBoth) { 
    // Get coarse and fine location updates. 
    mFineProviderButton.setBackgroundResource(R.drawable.button_inactive); 
    mBothProviderButton.setBackgroundResource(R.drawable.button_active); 
    // Request updates from both fine (gps) and coarse (network) providers. 
    gpsLocation = requestUpdatesFromProvider(
      LocationManager.GPS_PROVIDER, R.string.not_support_gps); 
    networkLocation = requestUpdatesFromProvider(
      LocationManager.NETWORK_PROVIDER, R.string.not_support_network); 

    // If both providers return last known locations, compare the two and use the better 
    // one to update the UI. If only one provider returns a location, use it. 
    if (gpsLocation != null && networkLocation != null) { 
     updateUILocation(getBetterLocation(gpsLocation, networkLocation)); 
    } else if (gpsLocation != null) { 
     updateUILocation(gpsLocation); 
    } else if (networkLocation != null) { 
     updateUILocation(networkLocation); 
    } 
} 
... 

それは最高のロケーションプロバイダのアイデアを実装する:あなたは(GPSからとネットワークから)両方のプロバイダを使用する場合、それは比較を行いますそのコードで

、以下のような時間の:完全なコードについては

/** Determines whether one Location reading is better than the current Location fix. 
    * Code taken from 
    * http://developer.android.com/guide/topics/location/obtaining-user-location.html 
    * 
    * @param newLocation The new Location that you want to evaluate 
    * @param currentBestLocation The current Location fix, to which you want to compare the new 
    *  one 
    * @return The better Location object based on recency and accuracy. 
    */ 
protected Location getBetterLocation(Location newLocation, Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     // A new location is always better than no location 
     return newLocation; 
    } 

    // Check whether the new location fix is newer or older 
    long timeDelta = newLocation.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    // If it's been more than two minutes since the current location, use the new location 
    // because the user has likely moved. 
    if (isSignificantlyNewer) { 
     return newLocation; 
    // If the new location is more than two minutes older, it must be worse 
    } else if (isSignificantlyOlder) { 
     return currentBestLocation; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(newLocation.getProvider(), 
      currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and accuracy 
    if (isMoreAccurate) { 
     return newLocation; 
    } else if (isNewer && !isLessAccurate) { 
     return newLocation; 
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
     return newLocation; 
    } 
    return currentBestLocation; 
} 

、上記のリンクを見てください

1

ちょうど任意の条件を与え、そして最高プロバイダを取得しようとしません。

LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 
String best = mgr.getBestProvider(criteria, true); 
//since you are using true as the second parameter, you will only get the best of providers which are enabled. 
Location location = mgr.getLastKnownLocation(best); 
+0

のように動作しません – silverFoxA

0

検索最高のロケーションプロバイダに対する下さいユーザーbelowe Cを。 ode: 理解するためにはhttps://developer.android.com/reference/android/location/Criteria.html を参照してください。基準はどのように機能するのですか?

public String getProviderName() { 
    LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setSpeedRequired(true); 
    criteria.setAltitudeRequired(false); 
    criteria.setBearingRequired(false); 
    criteria.setCostAllowed(false); 
    return locationManager.getBestProvider(criteria, true); 
}