2013-03-28 37 views
6

Tôi cần nhận các thay đổi về vị trí từ cả nhà cung cấp Mạng và GPS.Android LocationManager Tiêu chí

Nếu nhà cung cấp GPS không có sẵn hoặc không có vị trí (khả năng hiển thị sattelite xấu), tôi sẽ nhận vị trí từ nhà cung cấp mạng khác từ nhà cung cấp GPS.

Có thể chọn nhà cung cấp sử dụng tiêu chí theo nhu cầu của tôi không?

+0

nếu bạn đang sử dụng lớp Tiêu chí để tìm Nhà cung cấp tốt nhất có sẵn theo cấu hình thì sẽ dẫn đến bất kỳ ai từ NHÀ CUNG CẤP GPS, CUNG CẤP MẠNG hoặc THỤ ĐỘNG. và không có yêu cầu sử dụng lớp Tiêu chuẩnĐịa điểm cập nhật để nhận được mã hóa vị trí cho nhà cung cấp –

+0

LocationManager sẽ không thông báo cho tôi về Mạng và GPS cả trong một cuộc gọi lại? Tôi có cần sử dụng hai requestLocation() với các cuộc gọi lại khác nhau không? – Nik

+0

Để đăng ký người quản lý vị trí của bạn để nhận thông tin cập nhật, bạn phải cung cấp cho nhà cung cấp, bạn muốn nhận thông tin cập nhật từ ai. như 'mgr.requestLocationUpdates (LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this);' –

Trả lời

9

Trên thực tế Android Developers - Making Your App Location Aware có một mã ví dụ tuyệt vời để đáp ứng nhu cầu của bạn.

trong mã của nó, nếu bạn sử dụng cả các nhà cung cấp (từ GPS và từ mạng) nó sẽ làm một so sánh:

... 
} 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); 
    } 
} 
... 

Nó thực hiện các ý tưởng của các nhà cung cấp vị trí tốt nhất (bằng cách xác định tính chính xác trong thời hạn quy định thời gian như:

/** 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; 
} 

Đối với mã thành công, hãy có một cái nhìn tại liên kết cung cấp ở trên

1

Chỉ cần đừng đưa ra bất cứ tiêu chí, và cố gắng để có được các nhà cung cấp tốt nhất :

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

tính năng này không hoạt động – silverFoxA

0

Đối với tìm kiếm tốt nhất vị trí nhà cung cấp xin vui lòng sử dụng belowe c. ode: Vui lòng tham khảo https://developer.android.com/reference/android/location/Criteria.html để hiểu, Tiêu chí sẽ hoạt động như thế nào?

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); 
} 
Các vấn đề liên quan