I'm new to android and I'm creating an app that will calculate the distance base from starting latitude and longitude to current latitude and longitude. Below is my code on getting the distance from latitude and longitude where start means starting and curr means current. (I just copied the formula on how to get the distance from two latitude and longitude on the internet)
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);
}
And this is my onLocationChanged function where it updates the latitude and longitude when the device has been moved and I am also setting the distance here.
@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");
}
Whenever I launch the app, It functions normally but when it start getting the location, It always crashes. Any idea? Please help. Thanks
This is the log
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)
This is the line 196 on my MainActivity.java
Double startLat = Double.parseDouble(startingLatitude.getText().toString());