【问题标题】:App crashes when getting the distance from two latitude and longitude从两个纬度和经度获取距离时应用程序崩溃
【发布时间】:2016-10-19 06:50:26
【问题描述】:

我是 android 新手,我正在创建一个应用程序来计算从起始纬度和经度到当前纬度和经度的距离基准。下面是我获取纬度和经度距离的代码,其中 start 表示开始,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());

【问题讨论】:

  • 崩溃日志会有所帮助,请发布以寻求帮助。
  • 请发表您的日志
  • 请发布日志
  • 伙计,这都是什么,你不需要这些,有一个简单的函数可以计算两个位置之间的距离double distance=location1.distanceTo(location2)

标签: android distance


【解决方案1】:

试试下面的代码。 android 中的Location 类具有内置方法 来获取两个位置之间的距离。 这将为您提供以米为单位的距离。

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)

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2014-08-29
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多