我正在开发1.5 Android应用程序。在Windows XP上使用Eclipse 3.4.2进行开发。我有一个MapView,请求更新等。
问题是在第一次手动注入GPS坐标后,应用程序停止识别已发送GPS坐标。
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MapController mc = mapView.getController();
TextView locationText = (TextView) findViewById(R.id.LocationBar);
LocationListener locationListener = new MyLocationListener(mc, itemizedOverlay, locationText);
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
然后,MyLocationListener只是更改TextView中的值以匹配新的GPS坐标。
public void onLocationChanged(Location loc) {
if (loc == null) {
return;
}
double lat = loc.getLatitude();
double lng = loc.getLongitude();
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
itemizedOverlay.addOverlay(new OverlayItem(p, "title", "snippet"));
String location = String.format("%f lat %f long", lat, lng);
locationText.setText(location);
}
我在onLocationChanged方法中添加了一些日志记录,它只在我第一次尝试发送更新时才看到位置。所有后续的都不会触发onLocationChanged方法。
附加信息:
logcat输出如下:
10-02 17:22:34.423: INFO/gps(6671): Provider gps is has status changed to 1. Extras: Bundle[mParcelledData.dataSize=52]
第一次GPS更新是假的:
10-02 17:22:49.383: INFO/gps(6671): Location provided by location provider: Location[mProvider=gps,mTime=-1000,mLatitude=25.0,mLongitude=23.0,mHasAltitude=true,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=false,mAccuracy=0.0,mExtras=Bundle[mParcelledData.dataSize=52]]
10-02 17:22:49.444: INFO/gps(6671): Provider gps is has status changed to 2. Extras: Bundle[mParcelledData.dataSize=52]
根据http://developer.android.com/reference/android/location/LocationProvider.html#AVAILABLE的说法,那个2映射到“可用”。
只要“可用”设置完毕,就不会通过其他位置。似乎有点违反直觉。
我相信您遇到了仿真器GPS驱动程序中的错误。
1.5版SDK的解决方法来自Google Issue Tracker#39。
在模拟器中,在主屏幕上,按
菜单 - >设置 - >日期和时间 - >(取消选中)自动 - >选择时区
并选择合适的时区(即您的)。
fix包含在SDK的1.6#43版本中。