Polylines appearing on map where they shouldn'

2019-02-07 12:21发布

问题:

I have a array of about 7000 locations, each one was recorded using the location manager on android, when loading these locations I filter out any that are further then 1km from the previous or have an accuracy higher then 50 using this:

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());

So that removes the possibilities for these random lines assuming its working as it should.

When it is working correctly it should come up like this:

However, when I zoom in a little more or move around sometimes I get these random lines:

Im adding the lines like this:

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);

The bar at the bottom is a selector to only show that span of locations (Because when you have something like 7000 locations showing then it gets pretty crazy and you get StackOverflowError's)

回答1:

There appears to be a bug open for it: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5313

EDIT

It appears if you filter vertices that are closer than 1 meter to each other the the bug is resolved. I will write some code to fix this later tonight and put it here.

UPDATE

This bug was handled in Google issue tracker

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

It was fixed in Google Play Services - 9.2.56

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



回答2:

Here is an approach that fixed this issue for us. Not necessarily the ideal solution, but it works. At certain zoom levels you may see a polyline extend beyond where it should be (like the pictures show above). If you keep changing your zoom level the extension would disappear and then reappear. This happened to us everytime when two coordinates were exactly the same. e.g.

Lets say you have 3 coordinates A,B,C. Bus starts from A and goes to B, the bus then turns around and comes to C. If A and C are the exact same coordinates you would see this polyline extension problem.

After a few approaches we settled on offseting the latitude of point C by .00001. That has fixed all our issues.