从内置方法 distanceTo 获取错误的距离
Getting Wrong distance from the built in method distanceTo
我正在尝试从经纬度获取距离,但是打击方法returns距离不对我该怎么办?
@Override
public void onLocationChanged(Location location) {
currentLatitude2 = location.getLatitude();
currentLongitude2 = location.getLongitude();
Location locationA = new Location("point A");
locationA.setLatitude(currentLatitude);
locationA.setLongitude(currentLongitude);
Location locationB = new Location("point B");
locationB.setLatitude(currentLatitude2);
locationB.setLongitude(currentLongitude2);
float distance = locationA.distanceTo(locationB);
Toast.makeText(this, distance + "", Toast.LENGTH_LONG).show();
}
第一次调用此函数获取经纬度
public void getlatlong() {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
} else {
//If everything went fine lets get latitude and longitude
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show();
}
}
//call this function to get latitude and longitude 2nd time and this will also return you the distance
// Note* to check the distance you should at least cover the distance 1 or half km
public void getlatlong2() {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
} else {
currentLatitude2 = location.getLatitude();
currentLongitude2 = location.getLongitude();
Location locationA = new Location("point A");
locationA.setLatitude(currentLatitude);
locationA.setLongitude(currentLongitude);
Location locationB = new Location("point B");
locationB.setLatitude(currentLatitude2);
locationB.setLongitude(currentLongitude2);
float distance = locationA.distanceTo(locationB)/1000;
// float a = (float) (distance * 0.000621371);
Toast.makeText(this, distance + "", Toast.LENGTH_LONG).show();
dist.setText(distance + "");
}
}
我正在尝试从经纬度获取距离,但是打击方法returns距离不对我该怎么办?
@Override
public void onLocationChanged(Location location) {
currentLatitude2 = location.getLatitude();
currentLongitude2 = location.getLongitude();
Location locationA = new Location("point A");
locationA.setLatitude(currentLatitude);
locationA.setLongitude(currentLongitude);
Location locationB = new Location("point B");
locationB.setLatitude(currentLatitude2);
locationB.setLongitude(currentLongitude2);
float distance = locationA.distanceTo(locationB);
Toast.makeText(this, distance + "", Toast.LENGTH_LONG).show();
}
第一次调用此函数获取经纬度
public void getlatlong() {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
} else {
//If everything went fine lets get latitude and longitude
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show();
}
}
//call this function to get latitude and longitude 2nd time and this will also return you the distance
// Note* to check the distance you should at least cover the distance 1 or half km
public void getlatlong2() {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
} else {
currentLatitude2 = location.getLatitude();
currentLongitude2 = location.getLongitude();
Location locationA = new Location("point A");
locationA.setLatitude(currentLatitude);
locationA.setLongitude(currentLongitude);
Location locationB = new Location("point B");
locationB.setLatitude(currentLatitude2);
locationB.setLongitude(currentLongitude2);
float distance = locationA.distanceTo(locationB)/1000;
// float a = (float) (distance * 0.000621371);
Toast.makeText(this, distance + "", Toast.LENGTH_LONG).show();
dist.setText(distance + "");
}
}