How to get longitude & latitude position in android programatically?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How do I get the current GPS location programmatically 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 31 32 33 34 35 36 37 38 39 40 41 42 | LocationManager locManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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 fromLat = roundTwoDecimals(location.getLatitude()); double fromLong = roundTwoDecimals(location.getLongitude()); } } // call location listener private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(),"GPS is Disabled.Please enable GPS", Toast.LENGTH_SHORT ).show(); updateWithNewLocation(null); } public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(),"GPS is Enabled", Toast.LENGTH_SHORT).show(); } public void onStatusChanged(String provider, int status, Bundle extras) { } }; |