我是Android的新手,我正在创建一个应用程序,用于计算从开始纬度和经度到当前纬度和经度的距离。下面是关于获取纬度和经度距离的代码,其中开始意味着开始,而curr意味着当前。 (我刚刚复制了关于如何在互联网上获得两个纬度和经度距离的公式)
public Double getDistance(Double startLat, Double startLong, Double currLat, Double currLong) {
int radius = 6371; //This is the radius of the earth
Double distanceLatitude = degreeToRadius(currLat-startLat);
Double distanceLongitude = degreeToRadius(currLong-startLong);
Double area =
Math.sin(distanceLatitude/2) * Math.sin(distanceLatitude/2) +
Math.cos(degreeToRadius(startLat)) * Math.cos(degreeToRadius(currLat)) *
Math.sin(distanceLongitude/2) * Math.sin(distanceLongitude/2)
;
Double circumference = 2 * Math.atan2(Math.sqrt(area), Math.sqrt(1-area));
Double totalDistance = radius * circumference;
return totalDistance;
}
public Double degreeToRadius(Double degreeToRadius) {
return degreeToRadius * (Math.PI/180);
}
这是我的onLocationChanged函数,它在设备移动时更新纬度和经度,我也在这里设置距离。
@Override
public void onLocationChanged(Location location) {
String locationMessage = "Latitude: " + location.getLatitude()
+ "Longitude: " + location.getLongitude();
Double locationLatitude = location.getLatitude();
Double locationLongitude = location.getLongitude();
Toast.makeText(getBaseContext(), locationMessage, Toast.LENGTH_LONG).show();
currentLatitude.setText(locationLatitude.toString());
currentLongitude.setText(locationLongitude.toString());
Double startLat = Double.parseDouble(startingLatitude.getText().toString());
Double startLong = Double.parseDouble(startingLongitude.getText().toString());
Double currLat = Double.parseDouble(currentLatitude.getText().toString());
Double currLong = Double.parseDouble(currentLongitude.getText().toString());
distance.setText(getDistance(startLat, startLong, currLat, currLong).toString() + "KM");
}
每当我启动应用程序时,它都能正常运行,但是当它开始获取位置时,它总是会崩溃。任何的想法?请帮忙。感谢
这是日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.webglider.fitnessapp, PID: 7600
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.webglider.fitnessapp.MainActivity.onLocationChanged(MainActivity.java:196)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:290)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:219)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:235)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是我的MainActivity.java上的第196行
Double startLat = Double.parseDouble(startingLatitude.getText().toString());
答案 0 :(得分:0)
尝试以下代码。 android中的位置类具有内置方法以获取两个位置之间的距离。 这将为您提供以米为单位的距离。
Location oldLocation = new Location("oldLocation");
oldLocation.setLatitude(oldLat);
oldLocation.setLongitude(oldLng);
Location newLocation = new Location("newLocation");
newLocation.setLatitude(newLat);
newLocation.setLongitude(newLng);
float distance = oldLocation.distanceTo(newLocation) ;
有关参考,请查看官方文档。https://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)