1

我想确定我当前的位置(纬度、经度和高度)。在我的数据库中,我有一个按表位置(id、名称、地址、经度、纬度、海拔)的表内容

如何确定我当前的位置和最近的位置?

我在 manifest.xml 中添加了这个:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

我如何使用方法 (.getlongitude(); .getLatitude(); .getaltitude()

文档中,这并不清楚。

先感谢您。

4

2 回答 2

4

您可以选择获取最后一个已知位置,或收听更新。最简单的方法是获取最新的:

位置 lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

如果你想要一个准确的位置,你需要使用 requestLocationUpdates() 来监听更新。

另外,请记得检查 GPS 设备是否已启用,请参阅如何确定 Android 设备的 GPS 是否已启用

要查找最近的地址,请参阅http://developer.android.com/reference/android/location/Geocoder.html

于 2011-03-13T11:34:58.267 回答
3

首先启用Ur GPS。

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    public static double lontitube,latitude;

    protected LocationManager locationManager;
    protected Location currentLocation;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );
}

 protected void performReverseGeocodingInBackground() {
        showCurrentLocation();

    }

    protected void showCurrentLocation() {

        currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (currentLocation != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    currentLocation.getLongitude(), currentLocation.getLatitude()

            );
            lontitube=currentLocation.getLongitude();
            latitude=currentLocation.getLatitude();

            System.out.println("----lati----longi----"+latitude+"\n"+lontitube);

               Toast.makeText(Demo.this, message,
               Toast.LENGTH_LONG).show();
        }

    }   

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {

       String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(Demo.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(Demo.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(Demo.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(Demo.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }

在 AndroidManifest 添加

ACCESS_FINE_LOCATION

GPS 使用许可。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
于 2011-04-20T06:44:02.683 回答