如何获得移动设备的经度和纬度的Andr​​oid? [英] How to get Latitude and Longitude of the mobile device in android?

查看:152
本文介绍了如何获得移动设备的经度和纬度的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用定位工具,我得到了移动设备的当前经度和纬度的Andr​​oid?

How do I get the current Latitude and Longitude of the mobile device in android using location tools?

推荐答案

使用的<一个href="http://developer.android.com/intl/de/reference/android/location/LocationManager.html"><$c$c>LocationManager.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

调用getLastKnownLocation()不阻塞 - 这意味着它会返回如果没有位置目前可用 - 所以你可能想看看传递<一个href="http://developer.android.com/intl/de/reference/android/location/LocationListener.html"><$c$c>LocationListener到<一个href="http://developer.android.com/intl/de/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.app.PendingIntent%29"><$c$c>requestLocationUpdates()法相反,这会给你的位置的异步更新。

The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

您需要给您的应用程序的<一个href="http://developer.android.com/intl/de/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION"><$c$c>ACCESS_FINE_LOCATION如果你想使用GPS权限的。

You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.

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

您可能还需要添加<一href="http://developer.android.com/intl/de/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION"><$c$c>ACCESS_COARSE_LOCATION许可因为当GPS不可用,并选择您所在的位置提供与<一个href="http://developer.android.com/intl/de/reference/android/location/LocationManager.html#getBestProvider%28android.location.Criteria,%20boolean%29"><$c$c>getBestProvider()方法。

You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.

这篇关于如何获得移动设备的经度和纬度的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆