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

发布于 2024-10-21 13:25:03 字数 651 浏览 2 评论 0原文

我想确定我当前的位置(纬度、经度和海拔)。 在我的数据库中,我有一个按表位置排列的表内容(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()

In 文档 这不清楚。

提前谢谢您。

I want to determine my curent position (latitude, longitude and altitude).
In my database, I have a table contents by table location (id,name,address,longitude,latitude,altitude)

How can I determine my curent position and the most near location?

I Added this in 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>

How can i use the methods (.getlongitude(); .getLatitude(); .getaltitude()

In documentation this is not clear.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煞人兵器 2024-10-28 13:25:03

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

Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

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

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

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

You can choose to either get the last known location, or listen for updates. The easiest way is to get the latest:

Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

If you want an accurate location, you need to listen forupdates using requestLocationUpdates().

Also, remember to check whether the GPS device is enabled, see How do I find out if the GPS of an Android device is enabled

To find the nearest addresses, see http://developer.android.com/reference/android/location/Geocoder.html

浅唱ヾ落雨殇 2024-10-28 13:25:03

首先启用您的 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

权限。

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

First Enable 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();
        }

    }

In AndroidManifest add

ACCESS_FINE_LOCATION

permission for GPS use.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文