2012-10-16 20 views
8

Tôi đang sử dụng API của Google và MapActivity, MapView vv ..
Khi tôi cần để có được vị trí hiện tại của tôi, tôi sử dụng mã này lần đầu tiên:Tôi có nên sử dụng Android LocationManager.getBestProvider mỗi lần tôi yêu cầu cập nhật vị trí không?

myLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
// Sets the criteria for a fine and low power provider 
Criteria crit = new Criteria(); 
crit.setAccuracy(Criteria.ACCURACY_FINE); 
crit.setPowerRequirement(Criteria.POWER_LOW);  
// Gets the best matched provider, and only if it's on 
String provider = myLocationManager.getBestProvider(crit, true); 
// If there are no location providers available 
if (provider == null){ 
    OnClickListener listener = new OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // Closes the activity 
      finish(); 
     } 
    }; 
    Utils.getInstance().showAlertDialog(this, R.string.warning_dialog_title, 
           R.string.no_location_providers_error_message, 
          android.R.drawable.ic_dialog_alert, false, listener); 
} 
// Location services is enabled 
else{ 
    myLocationListener = new MyLocationListener(); 
    myLocationManager.requestLocationUpdates(
      provider, 
      0, 
      0, 
      myLocationListener);   
    // TODO - put a marker in my location  
    GeoPoint currentGeoPoint = MapUtils.getInstance(). 
      getCurrentLocation(myLocationManager, provider);   
    // Center to the current location 
    centerLocation(currentGeoPoint, MAX_ZOOM); 
} 

Tôi cũng có một "trở lại vị trí hiện tại "- khi người dùng di chuyển bản đồ và muốn nhanh chóng quay lại vị trí hiện tại của mình.

Câu hỏi của tôi là, tôi có nên sử dụng phương pháp getBestProvider()mỗi lần Tôi muốn nhận thông tin vị trí?

tôi có thể lưu các nhà cung cấp choosen ngoái, nhưng các điều kiện có thể thay đổi mỗi giây:

  1. tài tắt GPS
  2. Không GPS tín hiệu
  3. NETWORK_PROVIDER là tốt hơn trong tình hình hiện nay
  4. vv ..

Cách tiếp cận phù hợp là gì?

+0

http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way- để-get-the-users-current-location-in-a/3145655 # 3145655. Kiểm tra liên kết. Bạn có thể tìm thấy câu trả lời cho câu hỏi của mình. – Raghunandan

+0

Điều này vẫn không trả lời được câu hỏi của tôi. Mã của tôi cũng hoạt động. Nhưng tôi nên hay tôi không nên gọi cho getBestProvider mỗi lần? Theo ý kiến ​​của tôi, có .. –

+0

http://developer.android.com/guide/topics/location/strategies.html#BestEstimate. Bạn có lẽ nên đi qua liên kết này. – Raghunandan

Trả lời

0

Bạn luôn có thể sử dụng điều này, nhưng hãy đảm bảo kiểm tra trên Location.getTime() -> điều này sẽ cho bạn biết thời gian đã được sửa và bạn có thể dễ dàng phân biệt giữa các bản sửa lỗi vị trí mới và cũ.

Các vấn đề liên quan