2010-02-12 237 views
4

我遇到問題。我想在android應用程序中查找當前位置的經度和緯度。當我用手機穩定時,我只需要它。找到當前位置的經緯度

我的代碼是:

LocationManager locationManager; 
    locationManager = (LocationManager)getSystemService 
    (Context.LOCATION_SERVICE); 
    Location location = locationManager.getLastKnownLocation 
    (LocationManager.GPS_PROVIDER); 


    locationManager.requestLocationUpdates(    
      LocationManager.GPS_PROVIDER,0,0,(LocationListener) this); 
    updateWithNewLocation(location); 
    } 

    private void updateWithNewLocation(Location location) { 
    TextView myLocationText = (TextView)findViewById(R.id.myLocationText); 

    String latLongString; 

    if (location != null) { 
    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    latLongString = "Lat:" + lat + "\nLong:" + lng; 
    } else { 
    latLongString = "No location found"; 
    } 

    myLocationText.setText("Your Current Position is:\n" + 
    latLongString); 
    } 

在模擬器中運行它之後我總是覺得「找不到位置」。爲什麼這樣呢?

+0

你能告訴我們你的代碼,所以我們可以看到,爲什麼它不工作?這個問題已經在StackOverflow上問過很多次了。做一個搜索,你可能已經找到了你的問題的答案。 – 2010-02-12 09:10:34

+0

你不會從你的模擬器獲得GPS。試試真正的設備。 – Dhruvisha 2012-05-04 09:19:04

回答

4

由於GPS收音機嚴重耗盡電池,所以無法一直保持GPS收音機。在您撥打getLastKnownLocation()時,收音機可能關閉。

你需要先做你的requestLocationUpdates(),然後等待修復提供給你的LocationListener。之後的任何時間(直到您removeUpdates()),getLastKnownLocation()應該返回一個非null值。

+0

「任何時候...直到你removeUpdates()','getLastKnownLocation()'應該返回一個非空值」 - 所以如果你調用removeUpdates()''getLastKnownLocation()'返回的位置將爲null事後?直到重新啓動後才能堅持下去?這是記錄在某處嗎? – 2013-07-18 15:13:26

+0

@Mr_and_Mrs_D:「所以如果我們調用removeUpdates(),那麼getLastKnownLocation()返回的位置將會爲空?」 - 這更多的是無法保證您將繼續獲得非'null'值。 「這是否記錄在某處?」 - 不,這是重點。只有當你知道設備正在主動尋找位置數據時,你纔會確信'getLastKnownLocation()'有可能返回非'null'數據。一旦*你*不再要求修復位置,所有投注都將關閉。 – CommonsWare 2013-07-18 15:19:37

+0

a-ha - 即使啓用了提供程序getLastKnownLocation()會在某個點後開始返回null(從上次請求位置更新的任何應用程序的位置更新中刪除,我猜) - 討論該問題的任何鏈接?人們會假設一旦獲得最後一個位置,這將保持可用直到重新啓動 - 或直到(所有?相關?)提供商_disabled_也許 – 2013-07-18 15:24:52

13
public class GPSmap extends Activity { 

    GeoPoint geoPoint; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LocationManager locManager; 
     setContentView(R.layout.main); 
     locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 
      500.0f, locationListener); 
     Location location = locManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location != null) { 
      double latitude = location.getLatitude(); 
      double longitude = location.getLongitude(); 
     } 
    } 

    private void updateWithNewLocation(Location location) { 
     TextView myLocationText = (TextView) findViewById(R.id.text); 
     String latLongString = ""; 
     if (location != null) { 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      latLongString = "Lat:" + lat + "\nLong:" + lng; 
     } else { 
      latLongString = "No location found"; 
     } 
     myLocationText.setText("Your Current Position is:\n" + latLongString); 
    } 

    private final LocationListener locationListener = new LocationListener() { 

     public void onLocationChanged(Location location) { 
      updateWithNewLocation(location); 
     } 

     public void onProviderDisabled(String provider) { 
      updateWithNewLocation(null); 
     } 

     public void onProviderEnabled(String provider) {} 

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

如果您將使用此代碼,您將得到答案。

+5

如果你將使用這段代碼,你會得到答案....但沒有接受....恥辱。 – Daniel 2012-07-17 17:38:13

1

模擬器默認情況下可能沒有任何關聯的位置。所以它沒有給予任何。

要發送位置,請轉到Eclipse中的DDMS透視圖,然後輸入緯度和經度,然後單擊發送。然後你就可以在你的應用程序中看到它。

讓我知道你是否仍然有任何問題。

1

我有很多搜索教程,但發現這個最好的一個:

使用CriteriaBestProvider

/** PROCESS for Get Longitude and Latitude **/ 
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 

// getting GPS status 
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
Log.d("msg", "GPS:"+isGPSEnabled); 

// check if GPS enabled  
if(isGPSEnabled){ 
    /* 
    longitude = 70.80079728674089; 
    latitude = 22.29090332494221; 
    */ 
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 

    //new LoadPlaces().execute(); 
    //Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if(location != null) 
    { 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
     Log.d("msg", "first lat long : "+latitude +" "+ longitude); 
     //new LoadPlaces().execute(); 
    }else 
    { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onLocationChanged(Location location) { 
       // TODO Auto-generated method stub 
       longitude = location.getLongitude(); 
       latitude = location.getLatitude(); 
       Log.d("msg", "changed lat long : "+latitude +" "+ longitude); 
      } 
     }); 
    } 

} 
else 
{ 
    showAlertforGPS(); 
} 
相關問題