获取Android上的当前GPS位置
我试图通过GPSfunction获取用户的当前位置,
写了一个实现LocationListener
的简单类
public class LocationManagerHelper implements LocationListener { private static double latitude; private static double longitude; @Override public void onLocationChanged(Location loc) { latitude = loc.getLatitude(); longitude = loc.getLongitude(); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } public static double getLatitude() { return latitude; } public static double getLongitude() { return longitude; } }
从一个简单的动作我正在访问这些经度和纬度值
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** create a TextView and write Hello World! */ TextView tv = new TextView(this); LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new LocationManagerHelper(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { tv.append("Latitude:- " + LocationManagerHelper.getLatitude() + '\n'); tv.append("Longitude:- " + LocationManagerHelper.getLongitude() + '\n'); } else { tv.setText("GPS is not turned on..."); } /** set the content view to the TextView */ setContentView(tv);
但结果总是返回0.0。不能解决问题。
在onCreate()返回之后,才能触发您的位置更新回调。 如果将lat / long变量初始化为虚拟值,您可能会看到正在打印这些值。
在onLocationChanged中添加一些日志记录,以便您可以看到它被触发,然后阅读一下android应用程序如何处理回调和更新UI。
还要确保您的应用程序在其清单中具有适当的权限。
位置更新实际上是异步的 。 这意味着API不会使您的调用线程等到新位置可用; 相反,您使用特定方法( 回调 )注册一个观察者对象,只要计算新位置就会调用该方法。
在Android LocationManager API中 ,观察者是LocationListener对象,位置更新的主要回调是onLocationChanged()
这是一个试图解释这一点的图表(希望这有助于而不是混淆你!)
所以从你目前的代码:
- 将mlocListener声明为Activity子类的成员
- 在LocationListener实现中添加日志输出(Logcat行)
- 保持其余代码不变。
- 如果尚未执行,则在清单中添加正确的权限(GPS需要FINE_LOCATION )。
- 尝试将手机连接到互联网和窗户附近,以获得相当快的GPS定位(应该是~30s)。
然后启动应用程序并观察logcat中发生的情况。 在初始请求之后,您将看到状态更改和位置更新不会立即显示,从而解释了textview始终显示的原因(0.0,0.0)。
更多: http : //developer.android.com/guide/topics/location/obtaining-user-location.html
获得mLocListener后 – 设置Criteria,如下所示
String mlocProvider;
Criteria hdCrit = new Criteria();
hdCrit.setAccuracy(Criteria.ACCURACY_COARSE);
mlocProvider = mlocManager.getBestProvider(hdCrit, true);
然后使用getLastKnownLocation
tv.append("\n\nLocations (starting with last known):");
Location currentLocation = mlocManager.getLastKnownLocation(mlocProvider);
确保你的清单中有这些
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
如果您正在使用模拟器 – 在DDMS透视图中,请在“模拟器控制”选项卡中查找位置控件。 然后使用“手动”选项卡设置“经度”和“纬度”,然后单击“发送” – 在程序运行时执行此操作,您将看到对onLocationchanged的调用。 登录onLocationChanged是个好主意。
顺便说一句,requestLocationUpdates中的参数设置为“… 0,0 ……” – 它会耗尽你的电池 – 我看到手机在6-8小时内死机 – 将其改为“…… 30000,100 …“ – millisec中的第一个参数,另一个以米为单位。
当你在模拟器上检查它时,它将给出0.0。 你必须将位置传递给模拟器。 你可以从C:\ android-sdk_r06 \ android-sdk-windows \ tools \ ddms.bat文件传递这个。
在ddms中有一个名为模拟器控件的选项卡。 从那里你可以将位置传递给模拟器。 尝试这个。 希望它会奏效。
如何创建应用程序?
按行动获取当前位置坐标(获取位置)纬度/经度
将位置发布到要在地图上显示的网络服务器。(提交)清除按钮以清除旧位置(清除位置)或打开(提交)它变得清晰(0.0.0.0)
在Android上获取当前的GPS位置 http://developer.android.com/guide/topics/location/obtaining-user-location.html