2012-12-11 63 views
5

好的,我花了大量的時間搜索和閱讀,而不僅僅是這個網站。大多數線程處理GPS或試圖讓它在模擬器中工作。 developer.android.com上的文檔只解釋了一點。我無法得到一個簡單的工作。與大多數遇到此問題的人一樣,我只想從網絡提供商處獲得最後一個已知位置,即使在我使用Google地圖(使用網絡提供商,因爲GPS已關閉)之後,它也不會得到它,並且它向我展示了只有1/8到1/4英里遠。好吧,所以「提供者」(手機信號塔/ Wi-Fi如果打開)正在修復Google地圖。我嘗試我的應用程序,無法獲取最後一個已知位置。我甚至沒有試圖在地圖中顯示它,我只是在按下按鈕來獲取位置後,將其經緯度和長座標打印到屏幕上。該文件沒有說明最後一個地點必須從同一活動中完成。它表示它將返回指定提供者的最後一個已知位置。我正在指定NETWORK_PROVIDER,文檔中提到的是手機信號塔和Wi-Fi。 (是的,我已在清單中列出)getLastKnownLocation from NETWORK_PROVIDER on phone returns null

有沒有人知道這是如何工作的?我還沒有看到一個很好的解釋,即使在谷歌的網站上。如果你說不出來,我很沮喪。這顯然是我正在做(或不是)的事情,所以我不會責怪除了我自己以外的任何人/別人,除非文檔對於我的需求來說太短了。

更新:我現在正在使用位置監聽器並獲取位置,我推測最後一個位置。似乎合乎邏輯的是能夠在沒有通過位置偵聽器啓動位置更新的情況下獲取最後一個位置,但我沒有,現在我正在使用位置偵聽器。

現在我的問題是我無法獲取位置改變。我的代碼現在不執行任何操作,除非嘗試通過偵聽器獲取位置更新,並且接受按鈕按下以搶奪最後一個已知位置。谷歌地圖會告訴我我在哪裏,但我一直在20英里外的地方找到我的代碼。我有一種感覺,我可能真的會得到一個新的位置,但我可能會錯誤地使用最終/靜態聲明,這些聲明使我無法在需要時存儲和使用該位置。我的清單具有internet和fine_location權限,活動的xml工作正常 - 它只是提供一個按鈕來打開/關閉位置的實時搜索,一個手動檢索lastknownlocation的按鈕以及一個顯示經緯度的文本視圖。我試圖去除非必需品以防止它太長。

public class RMain extends Activity { 

public boolean islivesearchon; 
public static double netlistentime = 0 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds 
public static double netlistendistance = 0 * 1609.344; // miles * conversion to meters 
public static double gpslistentime = 30 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds 
public static double gpslistendistance = 0 * 1609.344; // miles * conversion to meters 

private static Location currentlocation; 
private LocationManager locationManager; 
private LocationListener locationListener; 
public static Criteria criteria; 
public TextView latview; 
public TextView longview; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rmain); 

    latview = (TextView) findViewById(R.id.display_latitude); 
    longview = (TextView) findViewById(R.id.display_longitude); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    // setup bestProvider 
    criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    String bestprovider = locationManager.getBestProvider(criteria, true); 

    // get an initial current location 
    currentlocation = locationManager.getLastKnownLocation(bestprovider); 
    latview.setText(Double.toString(currentlocation.getLatitude())); 
    longview.setText(Double.toString(currentlocation.getLongitude())); 

    // Define locationListener interface 
    locationListener = new LocListener(); 

    // React to LiveSearch toggle button press 
    final Button livesearch = (Button) findViewById(R.id.button_livesearch); 
    livesearch.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      TextView livesearchview = (TextView) findViewById(R.id.button_livesearch); 
      boolean isNetEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
      boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

      if(islivesearchon) { 
       locationManager.removeUpdates(locationListener); 
       islivesearchon = false; 
       livesearchview.setText("Live Search is OFF"); 
      } 
      else { 
       // ideally should check for !isProviderEnabled first, then provide option for turning it on 
       if(isNetEnabled) { 
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, (long) netlistentime, (float) netlistendistance, locationListener); 
        islivesearchon = true; 
        livesearchview.setText("Live Search is ON"); 
       } 
       // ideally should check for !isProviderEnabled first, then provide option for turning it on 
       if(isGpsEnabled) { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) gpslistentime, (float) gpslistendistance, locationListener); 
        islivesearchon = true; 
        livesearchview.setText("Live Search is ON"); 
       } 
      } 
     } 
    }); 

    // Respond to Get Location button click to get last known location 
    final Button getlocation = (Button) findViewById(R.id.button_getlocation); 
    getlocation.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      String bestprovider = locationManager.getBestProvider(criteria, true); 
      Location lastlocation = locationManager.getLastKnownLocation(bestprovider); 

      latview.setText(Double.toString(lastlocation.getLatitude())); 
      longview.setText(Double.toString(lastlocation.getLongitude())); 
     } 
    }); 

} 

private class LocListener implements LocationListener { 

    @Override 
    public void onLocationChanged(Location latestlocation) { 
     // Called when a new location is found by the network location provider. 
     currentlocation.set(latestlocation); 
     latview.setText(Double.toString(currentlocation.getLatitude())); 
     longview.setText(Double.toString(currentlocation.getLongitude())); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 

    @Override 
    public void onProviderEnabled(String provider) {} 

    @Override 
    public void onProviderDisabled(String provider) {} 
}; 

}

+1

你在手機裏模擬嗎? – 2012-12-11 06:42:53

+1

只需在剛啓動應用程序時設置任何位置,然後檢索getLastKnownLocation ... –

+0

謝謝你們。是的,我只在手機內部模擬 - Eclipse中的模擬器並不總是適合我。 – crispy

回答

7

試着檢查這個...一旦你的onLocationChange被調用,之後你可以使用上一個已知位置獲得的舊值。

locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); 

     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location != null) { 

      old_latitude=location.getLatitude(); 
      old_longitude=location.getLongitude(); 
      Log.d("old","lat : "+old_latitude); 
      Log.d("old","long : "+old_longitude); 

      this.onLocationChanged(location); 
     } 
+0

我甚至還沒有放置onlocationchange方法。看起來我應該能夠從NETWORK_PROVIDER獲取最後一個已知位置,如文檔中所述,但我甚至無法得到。我現在要全力以赴,把所有的代碼放在最後,最好,最新等等的位置上,然後看看它是如何發展的。我正在學習JAVA,它試圖做文檔聽起來像是應該做的那樣令人沮喪。我想,如果我連這個簡單命令都無法完成,忘記所有其他位置的東西...... – crispy

+0

只需使用onlocationchanged()一次,之後,每次都會得到最後一次knownlocation()。直到再次發生位置變化。這將工作肯定:) – Rahul

4

它返回「空」,當服務在設置>位置和安全>位置通過網絡

以便能經常發生禁止。

+0

謝謝。我的手機有被選中的「Google位置服務」,當前未選中的「獨立GPS服務」以及被選中的「VZW位置服務」(Verizon提供的位置服務)。再次,谷歌將得到一個解決方案和無Wi-Fi(更好的Wi - Fi),並沒有GPS。我立即進入我的應用程序,我無法獲得最後的位置 - 始終爲0.0 lat和0.0 long。我開始認爲它提供了我的應用程序從指定提供程序檢索到的最後一個位置。我確實發現了另一個線程,因爲安全原因,有人提到這可能發生。 – crispy

+0

請對我的答案。並關閉問題 –

+0

好的答案..我在Micromax和三星上測試了這個。完善。 – Siddharth