2013-07-19 23 views
2

我要得到確切的&精確的電流位置更新的位置。爲此,我使用位置管理器requestLocationUpdates()方法,但調用onLocationChanged()方法將需要很多時間。所以我把計時器設置爲20秒。我的意思是onLocationChanged()方法甚至在20秒後不被調用,然後我決定採取最後一個已知位置。在這裏我正面臨着這個問題。 getLastKnownLocation()返回null。android-如何在不打開地圖/導航應用

但我想要的位置。在這裏我找到了一個解決方案。這是因爲測試設備沒有與gps提供商進行最近的位置更新。所以我們需要手動打開地圖/導航應用程序,然後它會返回位置。我們需要做些什麼來獲取位置,而不是打開地圖應用。我認爲我們應該像地圖應用那樣做位置更新。我們如何在不打開它的情況下實現這一目標。

//使用requestLocationUpdates

public void getLocation() { 

    locationManager.requestLocationUpdates(strProvider, 0, 0, 
      CurrentlocationListener); 

} 

LocationListener CurrentlocationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 
     if (location != null) { 

      locationManager.removeUpdates(this); 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      String strCurrentLatitude = String.valueOf(lat); 
      String strCurrentLongitude = String.valueOf(lng); 

      System.out 
         .println("Current Latitude and Longitude(onLocation Changed): " 
           + strCurrentLatitude 
           + "," 
           + strCurrentLongitude); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) {} 

    @Override 
    public void onProviderEnabled(String provider) {} 

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

獲取當前位置//獲取使用lastknownlocation當前位置。

String location_context = Context.LOCATION_SERVICE; 

    locationManager = (LocationManager) getSystemService(location_context); 
    Criteria crit = new Criteria(); 
    crit.setAccuracy(Criteria.ACCURACY_FINE); 
    strProvider = locationManager.getBestProvider(crit, true); 
    Location location = locationManager 
         .getLastKnownLocation(strProvider); 
       System.out.println("location(geoPoint1): " + location); 

       String strCurrentLatitude = "0", strCurrentLongitude = "0"; 

       if (location != null) { 
        strCurrentLatitude = String.valueOf(location 
          .getLatitude()); 
        strCurrentLongitude = String.valueOf(location 
          .getLongitude()); 
       } 
+0

顯而易見的方法是在關閉應用程序之前保存座標並在啓動後加載它們。看看這裏多了一個解決方案:http://stackoverflow.com/questions/9885871/emulate-a-broadcast-in-android – Vyacheslav

+0

@trololo:我想目前的位置,即使第一次應用在這種情況下,開放我們沒有座標正確。在這裏,我們應該如何獲得位置? – naresh

回答

0

無論你在做什麼OnLocationChanged()是正確的。現在用這個替換LocationManager的代碼。它會工作。

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 

      // Creating a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 

      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria, true); 

      // Getting Current Location 
      Location location = locationManager.getLastKnownLocation(provider); 

      if (location != null) { 
       onLocationChanged(location); 



      } 

      locationManager.requestLocationUpdates(provider, 1000, 0, this); 

     } 

謝謝。 :)

+0

我們如何調用onLocationChanged(位置)?在我的我得到這個錯誤方法onLocationChanged(位置)是未定義的類型RB_UpcomingExits – naresh

+0

我認爲,它會給你持續不斷的值,你不會在此後null。如果是。您可能需要將其標記爲答案。謝謝。 – TheLittleNaruto

相關問題