onLocationChanged returns 旧位置
onLocationChanged returns old location
在我跟踪行进距离的应用程序中,我有两个按钮。第一个开始位置更新,第二个停止。
public void start(View view){
cords = new ArrayList<LatLng>();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED){
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
}
public void stop(View view) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
然后我声明 onLocationChanged 我计算用户已经走过的距离。
@Override
public void onLocationChanged(Location location) {
cords.add(new LatLng(location.getLatitude(), location.getLongitude()));
if (cords.size() > 1) {
Location previousLocation = new Location("");
previousLocation.setLatitude(cords.get(cords.size() - 2).latitude);
previousLocation.setLongitude(cords.get(cords.size() - 2).longitude);
stats.updateDistance(location.distanceTo(previousLocation));
}
}
问题是当我停止测量然后再次开始测量时,位置 returns 旧值。我怎样才能摆脱这个旧值并从全新的位置开始距离测量。
我发现我没有得到旧位置,而是通过网络而不是 gps 测量的位置,这就是为什么它如此不准确。当我远离建筑物时,gps 开始工作并且位置是正确的。因此,为了摆脱网络测量,我检查位置是否有 Altitude(),如果没有,则意味着它是由网络测量的,所以我可以忽略它。
@Override
public void onLocationChanged(Location location) {
if(location.hasAltitude()) {
cords.add(new LatLng(location.getLatitude(), location.getLongitude()));
if (cords.size() > 1) {
Location previousLocation = new Location("");
previousLocation.setLatitude(cords.get(cords.size() - 2).latitude);
previousLocation.setLongitude(cords.get(cords.size() - 2).longitude);
stats.updateDistance(location.distanceTo(previousLocation));
}
在我跟踪行进距离的应用程序中,我有两个按钮。第一个开始位置更新,第二个停止。
public void start(View view){
cords = new ArrayList<LatLng>();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED){
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
}
public void stop(View view) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
然后我声明 onLocationChanged 我计算用户已经走过的距离。
@Override
public void onLocationChanged(Location location) {
cords.add(new LatLng(location.getLatitude(), location.getLongitude()));
if (cords.size() > 1) {
Location previousLocation = new Location("");
previousLocation.setLatitude(cords.get(cords.size() - 2).latitude);
previousLocation.setLongitude(cords.get(cords.size() - 2).longitude);
stats.updateDistance(location.distanceTo(previousLocation));
}
}
问题是当我停止测量然后再次开始测量时,位置 returns 旧值。我怎样才能摆脱这个旧值并从全新的位置开始距离测量。
我发现我没有得到旧位置,而是通过网络而不是 gps 测量的位置,这就是为什么它如此不准确。当我远离建筑物时,gps 开始工作并且位置是正确的。因此,为了摆脱网络测量,我检查位置是否有 Altitude(),如果没有,则意味着它是由网络测量的,所以我可以忽略它。
@Override
public void onLocationChanged(Location location) {
if(location.hasAltitude()) {
cords.add(new LatLng(location.getLatitude(), location.getLongitude()));
if (cords.size() > 1) {
Location previousLocation = new Location("");
previousLocation.setLatitude(cords.get(cords.size() - 2).latitude);
previousLocation.setLongitude(cords.get(cords.size() - 2).longitude);
stats.updateDistance(location.distanceTo(previousLocation));
}