我正在使用 gpx 文件在模拟器上测试我的应用程序。坐标发生变化,但方位和速度始终为零。我检查了我的 LocationManager 设置,.supportsSpeed() 和 .supportsBearing() 都返回 true。
我的 GPS 代码与这个http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/几乎是一对一的,但这部分是最重要的,所以我把它贴在这里:
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
Log.d("GPS support",((Boolean)locationManager.getProvider(locationManager.GPS_PROVIDER).supportsBearing()).toString());
Log.d("GPS support",((Boolean)locationManager.getProvider(locationManager.GPS_PROVIDER).supportsSpeed()).toString());
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
我只添加了这段代码
@Override
public void onLocationChanged(Location location) {
this.location = location;
}
我在活动的 onCreate 方法中实例化 GPSTracker 对象,然后在需要的地方使用 gps.getLocation() 。
我用于测试的 .gpx 文件可在此处获得:http ://snk.to/f-cdzj45ic