地图上出现了不该出现的折线

3

我有一个包含大约7000个位置的数组,每个位置都是在Android上使用位置管理器记录的。在加载这些位置时,我使用以下方法过滤掉距离前一个位置超过1公里或者精度高于50的任何位置:

if (c.moveToFirst())
do {
    lat = c.getString(0);
    lng = c.getString(1);
    ac = c.getString(2);
    alt = c.getString(3);
    if (l1 != null) {
        l2 = new Location("MAP");
        l2.setLatitude(Double.parseDouble(lat));
        l2.setLongitude(Double.parseDouble(lng));
        l2.setAltitude(Double.parseDouble(alt));
        l2.setAccuracy(Float.parseFloat(ac));
        if (l1.distanceTo(l2) < 1000
                && l2.getAccuracy() < 51) {
            opts.add(new LatLng(Double.parseDouble(lat),
                    Double.parseDouble(lng)));
            list.add(l2);
            l1 = l2;
        }
    } else {
        l1 = new Location("MAP");
        l1.setLatitude(Double.parseDouble(lat));
        l1.setLongitude(Double.parseDouble(lng));
        l1.setAccuracy(Float.parseFloat(ac));
        l1.setAltitude(Double.parseDouble(alt));
        if (l1.getAccuracy() > 50)
            l1 = null;
    }

} while (c.moveToNext());

因此,这消除了这些随机线条的可能性,假设它按照应有的方式工作。

当它正常工作时,它应该像这样出现:

输入图像描述

然而,当我稍微放大一点或移动一下,有时会出现这些随机线:

输入图像描述 输入图像描述 输入图像描述 输入图像描述

我是这样添加这些线条的:

Location[] locations = Arrays.copyOfRange(mLocations, a, b);

if (mStartLine != null)
    mStartLine.remove();
if (mMiddleLine != null)
    mMiddleLine.remove();
if (mEndLine != null)
    mEndLine.remove();
if (mMarker != null) {
    mMarker.remove();
    mMarker = null;
}
PolylineOptions so = new PolylineOptions();
PolylineOptions mo = new PolylineOptions();
PolylineOptions eo = new PolylineOptions();

so.color(Color.GREEN);
eo.color(Color.GREEN);
mo.color(Color.BLACK);

if (locations.length < 2) {
    if (locations.length == 0)
        return;
    // Add just a dot instead.
    MarkerOptions m = new MarkerOptions();
    m.position(new LatLng(locations[0].getLatitude(), locations[0]
            .getLongitude()));
    mMarker = mMap.addMarker(m);
    return;
}
so.add(new LatLng(locations[0].getLatitude(), locations[0].getLongitude()));
so.add(new LatLng(locations[1].getLatitude(), locations[1].getLongitude()));
mStartLine = mMap.addPolyline(so);
for(int i = 1; i < (locations.length - 1); i++){
    mo.add(new LatLng(locations[i].getLatitude(), locations[i].getLongitude()));
}
mMiddleLine = mMap.addPolyline(mo);
eo.add(new LatLng(locations[locations.length - 2].getLatitude(), locations[locations.length - 2].getLongitude()));
eo.add(new LatLng(locations[locations.length - 1].getLatitude(), locations[locations.length - 1].getLongitude()));
mEndLine = mMap.addPolyline(eo);

底部的栏目是一个选择器,可以仅显示该位置的跨度(因为当您有大约7000个位置时,显示就会变得相当混乱,并得到StackOverflowError)。

2个回答

3

有一个与此相关的错误问题正在被处理中:https://code.google.com/p/gmaps-api-issues/issues/detail?id=5313

编辑

如果您过滤距离小于1米的顶点,则该错误会得到解决。我将编写一些代码来修复这个问题,并在晚些时候放在这里。

更新

此错误已在Google问题跟踪器中处理。

https://issuetracker.google.com/issues/35821816

它在Google Play Services - 9.2.56中得到了修复。

https://developers.google.com/maps/documentation/android-api/releases#june_27_2016


如果我找到答案,我会在这里发布。目前这就是我所拥有的。 - FabianCook
有关这个问题的任何解决方案? - ar-g

0

这里有一种方法可以解决我们遇到的问题。虽然不一定是最理想的解决方案,但它确实有效。在某些缩放级别下,您可能会看到折线延伸到不应该出现的位置(就像上面的图片所示)。如果您不断更改缩放级别,则延伸部分将消失,然后重新出现。每当两个坐标完全相同时,这种情况就会发生。例如:

假设您有3个坐标A、B、C。公交车从A出发,到达B,然后掉头回到C。如果A和C的坐标完全相同,您将看到这个折线延伸问题。

经过几种尝试,我们最终选择了将点C的纬度偏移0.00001。这解决了我们所有的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接