2011-08-25 135 views
0

即時通訊使用經緯度/ lng來查找我的用戶在哪裏,並計算他和地圖中另一個地方之間的距離(其lat/lng是靜態的)。爲了做到這一點,我使用:刷新經緯度或加載更快

private void findloc() { 
      // TODO Auto-generated method stub 

      try{ 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      Criteria criteria = new Criteria(); 
      provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 
      Double lat = Double.valueOf(location.getLatitude()); 
      Double lng = Double.valueOf(location.getLongitude()); 

      Location location2 = new Location("gps"); 
      location2.setLatitude(35.519583); 
      location2.setLongitude(24.016864); 


      float distance = location2.distanceTo(location); 
      TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm); 
      sitesdistancekm.setText("Geographical Distance : "+distance/1000+" km"); 


      Toast.makeText(otto.this, "lat: "+lat+"\nlng: "+lng, 
      Toast.LENGTH_LONG).show(); 
     } 
      catch(Exception e){ 

       TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm); 
       sitesdistancekm.setText("No GPS signal");  


      } 
     } 

我的問題是,有時位置堆棧和即時得到一個錯誤的距離.IM調用findloc()作爲菜單的刷新,但它不改變。

public boolean onOptionsItemSelected(MenuItem item) { 

       findloc(); 

     return true; 
    } 

有什麼辦法可以改進它嗎?

編輯

這樣會行嗎?

private void findloc() { 
      // TODO Auto-generated method stub 

      try{ 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      Criteria criteria = new Criteria(); 
      provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 
      Double lat = Double.valueOf(location.getLatitude()); 
      Double lng = Double.valueOf(location.getLongitude()); 

      location2 = new Location("gps"); 
      location2.setLatitude(35.519583); 
      location2.setLongitude(24.016864); 


      float distance = location2.distanceTo(location); 
      TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm); 
      sitesdistancekm.setText("Geographical Distance : "+distance/1000+" km"); 


      Toast.makeText(otto.this, "lat: "+lat+"\nlng: "+lng, 
      Toast.LENGTH_LONG).show(); 


      onLocationChanged(location); 

     } 
      catch(Exception e){ 

       TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm); 
       sitesdistancekm.setText("No GPS signal");  


      } 
     } 

     private void onLocationChanged(Location location) { 
     // TODO Auto-generated method st 


      float distance = location2.distanceTo(location); 
      TextView sitesdistancekm = (TextView) findViewById(R.id.sitesdistancekm); 
      sitesdistancekm.setText("Geographical Distance : "+distance/1000+" km"); 
    } 

回答

2

我發現getLastKnownLocation()不像註冊位置偵聽器那麼可靠。請參閱this以獲取有關如何實施的信息。

getLastKnownLocation文檔狀態:

返回一個位置表示從最後已知位置 數據弄不好從給定的供應商處獲得。這可以在沒有 啓動提供程序的情況下完成。請注意,如果設備已關閉並移動到另一個 位置,則該位置可能已過時,例如 。

對我來說,我使用getLastKnownLocation()來獲取初始位置,但是然後爲其他任何事件註冊一個偵聽器。

+0

IIRC,該文件提到之前你提供位置更新的地方,'getLastKnownLocation()'可以返回不準確的結果在某些情況下,例如在一段時間內GPS尚未打開的情況下。 – thegrinner

+0

@jack可能我有一個示例代碼? – kostas

+0

我已經嘗試過它與位置監聽器,但這種方式需要更多的時間來找到我的經緯度/我的使用.... – kostas

1

參考Jack的回答,getLastKnownLocation不應該被用作位置檢測的唯一度量。您需要將位置檢測邏輯分爲幾個步驟:

  • 獲取你的最佳供應商的詳細準確


    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 

    // you need to specify the criteria here 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    String bestProvider = lm.getBestProvider(criteria, true); 

  • 但是你不能只使用最後一個已知的位置不變。您需要檢查此位置最後一次修復的時間。根據您需要的靈敏度,您可以檢查30秒到5分鐘之間的任何時間。

    Location locate = lm.getLastKnownLocation(bestProvider); 
    if(System.currentTimeMillis() - locate.getTime() < TIME_TO_EXPIRE) { 
     // this is a valid location, let's use this location 
     // as the last time the provider check is within reasonable boundary 
    } 
 
  • 如果最近一次的更新已通過您的預計時間界限,你可以假設的位置太舊(用戶移動到不同的地方),需要更新。爲你的下一步是通過調用位置更新請求如下

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
MIN_TIME, MIN_DISTANCE, locationListener); 

正如你所看到的,你沒有手動調用onLocationChange,而是讓供應商要求聽者如果MIN_TIME和MIN_DISTANCE有已超過。

  • 請注意,如果你的位置沒有改變,那麼onLocationChanged()將不會被調用。如果某段時間過去了,onLocationChanged沒有被調用,那麼您可以假設您最後一次知道的位置是最準確的位置,您應該使用它。

  • 也有可能onLocationChanged()不會被調用,因爲提供者無法獲得您的位置的修復。在這種情況下,可選地,你可以調用其他提供,看看他們是否能在最後已知位置靠背部