优化坐标距离计算

huangapple go评论60阅读模式
英文:

Optimize coordinates distance calculation

问题

你好,我需要帮助优化我的距离计算。
经过一段时间,可能是几分钟,也可能是几小时,这取决于手机的性能,应用程序会变得很慢,几乎无法运行。我已经将问题缩小到了距离计算上,但我不知道如何修复它。
我已将应用程序中的所有其他十进制数转换为浮点数,这在一定程度上有所帮助,但是我仍然需要以双精度(double)形式获得距离。
以下是代码:

double getTripDistance(List<LatLng> vertices) {

    double totalDistance = 0;

    for (int i = 0; i < vertices.size() - 1; i++) {
        Location tLoc1 = new Location("");
        Location tLoc2 = new Location("");

        tLoc1.setLatitude(vertices.get(i).latitude);
        tLoc1.setLongitude(vertices.get(i).longitude);

        tLoc2.setLatitude(vertices.get(i + 1).latitude);
        tLoc2.setLongitude(vertices.get(i + 1).longitude);

        totalDistance += tLoc1.distanceTo(tLoc2);
    }
    return totalDistance;
}
英文:

Hi guys i need help optimizing my distance calculation.
After some time may be minutes may be hours depends on the phone it slows it down and the app can barely work i have narrowed it down to the distance calculation but i dont know how to fix it.
I converted all of the oter decimal numbers in the app to float and that helped a little but i need the distance in double.
Here is the code

`double getTripDistance(List<LatLng> vertices) {

    double totalDistance = 0;

    for (int i = 0; i &lt; vertices.size() - 1; i++) {
        Location tLoc1 = new Location(&quot;&quot;);
        Location tLoc2 = new Location(&quot;&quot;);

        tLoc1.setLatitude(vertices.get(i).latitude);
        tLoc1.setLongitude(vertices.get(i).longitude);

        tLoc2.setLatitude(vertices.get(i + 1).latitude);
        tLoc2.setLongitude(vertices.get(i + 1).longitude);

        totalDistance += tLoc1.distanceTo(tLoc2);
    }
    return totalDistance;
}`

答案1

得分: 1

你不应该有一个List vertices,并且每次都从头开始计算基于该列表的距离。相反,你应该跟踪overallDistance,并且一旦向vertices列表中添加了一个新的vertex,你就获取该vertex和列表中的前一个条目,计算这两者之间的距离,并将结果添加到已有的overallDistance中。

伪代码:

class Trip {
    List vertices;
    double distance;

    addVertex(vertex) {
        last = vertices.getLast()
        vertices.add(vertex)
        if last {
             distance += distanceBetween(vertex, last)
        }
    }

    getTripDistance() {
        return distance;
    }
}
英文:

You should not have a List vertices and compute the distance based on that list from scratch every time. Rather you should track the overallDistance and as soon as a new vertex is added to the vertices list you take that one and the previous entry in the list, calculate the distance between those two and add the result onto the already existing overallDistance.

Pseudo code:

class Trip {
    List vertices;
    double distance;

    addVertex(vertex) {
        last = vertices.getLast()
        vertices.add(vertex)
        if last {
             distance += distanceBetween(vertex, last)
        }
    }

    getTripDistance() {
        return distance;
    }
}

huangapple
  • 本文由 发表于 2020年9月8日 19:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63793079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定