[英]wrong distance between 2 GPS points - android
I want to get the distance between 2 points, one is getted by GPS and the second is fix. 我想得到两点之间的距离,一是被GPS获取,第二是修复。 I get a result, but the distance is 40 km but it should be around 25 km and I can't find another formula.
我得到一个结果,但是距离是40公里,但应该在25公里左右,我找不到其他公式。 Please help me to fix the problem.
请帮助我解决问题。
btnShowLocation = (Button) getView().findViewById(R.id.btnLocation); btnShowLocation =(按钮)getView()。findViewById(R.id.btnLocation);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Objekt in der Klasse erzeugen
gps = new GPSTracker(getActivity());
//überprüfen, ob GPS aktiv ist
if(gps.canGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Toast.makeText(getActivity(), "Deine Location - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
//wenn keine Location gefunden wurde
// GPS oder Netzwerk nicht verfügbar
// User fragen, ob er die Einstellungen ändern will
gps.showSettingsAlert();
}
}
});
btnEntfernung = (Button) getView().findViewById(R.id.btnDistance);
btnEntfernung.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
double x1,y1,x2,y2,radius;
latitudeDB = Math.toRadians(48.569601);
longitudeDB = Math.toRadians(16.572087);
double entfernung=0;
radius= 6378.137/360;
x2=latitudeDB;
y2=longitudeDB;
x1=latitude;
y1=longitude;
double distanz =(x1-x2)+(y1-y2);
double distanzfaktor = Math.acos((Math.sin(y1)*Math.sin(y2))+((Math.cos(y1)*Math.cos(y2))* (Math.cos(x2)-Math.cos(x1))));
if (distanz >=0){
entfernung = radius*distanzfaktor;
}
else if(distanz < 0){
entfernung = radius*(distanzfaktor + Math.PI);
}
Toast.makeText(getActivity(), "Deine Entfernung" + entfernung + " km", Toast.LENGTH_LONG).show();
}
});
Why not use the built-in Location
class to do this for you? 为什么不使用内置的
Location
类为您执行此操作?
x2=latitudeDB;
y2=longitudeDB;
x1=latitude;
y1=longitude;
float[] results = new float[1];
Location.distanceBetween(x1, y1, x2, y2, results);
float distanceInMeters = results[0];
float distanceInKm = distanceInMeters / 1000.0f;
Toast.makeText(getActivity(), "Deine Entfernung" + distanceInKm + " km", Toast.LENGTH_LONG).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.