find当前位置的纬度和经度

我有个问题。 我想在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); } 

在模拟器上运行后,我总是发现“找不到位置”。 为什么这样呢?

 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){} }; } 

如果您将使用此代码,您将得到答案。

GPS收音机不会一直保持,因为它会严重损耗电池。 在调用getLastKnownLocation() ,收音机可能closures。

您需要先执行requestLocationUpdates() ,然后等待修复程序提供给您的LocationListener 。 任何时候(直到你removeUpdates() ), getLastKnownLocation()应该返回一个非null值。

仿真器默认情况下可能没有任何关联的位置。 所以它不给任何。

要发送位置,请在Eclipse中转到DDMS视angular,然后input纬度和经度,然后单击发送。 那么你将能够在你的应用程序中看到。

如果您还有任何问题,请告诉我。

我有很多的教程,但发现这个最好的一个:

使用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(); }