find current location latitude and longitude in Android
本问题已经有最佳答案,请猛点这里访问。
我想在Android应用程序中找到我当前位置的纬度和经度。 当我用手机稳定时,我只需要它。
我的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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 +" Long:" + lng; } else { latLongString ="No location found"; } myLocationText.setText("Your Current Position is: " + latLongString); } |
在模拟器上运行后,我总是找到"找不到位置"。 为什么会这样?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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 +" Long:" + lng; } else { latLongString ="No location found"; } myLocationText.setText("Your Current Position is: " + 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收音机不会一直保持开启状态,因为它会严重耗尽电池电量。 在您拨打
您需要先执行
我有很多教程,但发现这个最好的一个:
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /** 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(); } |
默认情况下,模拟器可能没有任何关联的位置。 所以它没有给予任何。
要发送位置,请转到Eclipse中的DDMS透视图,然后输入纬度和经度并单击"发送"。 然后,您将能够在您的应用程序中看到它。
如果您还有任何问题,请告诉我。