-3

可能重复:
如何在 Android 中以编程方式获取当前 GPS 位置?

我只想知道如何在android中以编程方式找出当前位置的纬度和经度坐标。我已经从这个论坛尝试了很多帖子,但无法找到解决方案。提前致谢。

4

1 回答 1

0
 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) {

    }
};
于 2012-08-01T07:10:57.867 回答