如何确定我当前的位置(经度,纬度和海拔高度)?

时间:2011-03-13 11:25:44

标签: android geolocation gps android-location

我想确定我的位置(纬度,经度和海拔高度)。 在我的数据库中,我按表位置(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()

documentation中,这一点尚不清楚。

提前谢谢。

2 个答案:

答案 0 :(得分:4)

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

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

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

此外,请记得检查GPS设备是否已启用,请参阅How do I find out if the GPS of an Android device is enabled

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

答案 1 :(得分: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" />