Mudasir Sharif
Mudasir Sharif

Reputation: 731

GoogleMaps draw multiple polylines even i am removing the previous polyline before plotting new one

I am using google maps and drawing polylines on it by passing array of coordinates on each location update. After plotting the polyline i first remove the previous one and then i am plotting new polyline based on updated coordinates array list. Problem is, google maps is plotting a separate polyline from starting to end point on each location update. I know mistake is somewhere in the hierarachy of calling things. Can anybody help me figuring this out.

This is the method in which i am updating the camera focus and plotting polylines.

private void moveCamera(Location location) {
        if (location != null) {
            if (polyline != null) {
                polyline.remove();
            }
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 16));


            coordList.add(new LatLng(location.getLatitude(), location.getLongitude()));

// Create polyline options with existing LatLng ArrayList
            polylineOptions.addAll(coordList);
            polylineOptions
                    .width(5)
                    .color(Color.RED);

// Adding multiple points in map using polyline and arraylist
            polyline = mMap.addPolyline(polylineOptions);

        }
    }

Upvotes: 0

Views: 1228

Answers (2)

Mudasir Sharif
Mudasir Sharif

Reputation: 731

Got the Solution. I was using the same old PolylineOptions again and again by declaring it as global variable. Now before each plot i create a new instance of PolylineOptions and now good to go.

Here is the updated code.

private void moveCamera(Location location) {
        if (location != null) {
            if (polyline != null) {
                polyline.remove();
            }
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 16));


            coordList.add(new LatLng(location.getLatitude(), location.getLongitude()));

polyline = mMap.addPolyline(new PolylineOptions().addAll(coordList)
                    .width(5)
                    .color(Color.RED));

        }
    }

Upvotes: 0

kemdo
kemdo

Reputation: 1459

just call

polylineOptions.clear(); -> delete current object

and

map.clear() -> invalidate map

before add new polyline

Upvotes: 0

Related Questions