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

まず、UrGPSを有効にします。

    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 に答える