googlemaps-samples/android-samples

Polyline add dynamically line color based on location not working.

mintheinwin opened this issue · 3 comments

Hello, I attempt to dynamically set the polyline color based on location. I've tried calling the method like this, but it doesn't seem to be functioning.

    Polyline line = map.addPolyline(new PolylineOptions()
                        .add(new LatLng(1.4384168567466844, 103.80680969583258), new LatLng(1.4408750600926357,    103.80097019583253))
                        .addSpan(new StyleSpan(Color.RED))
                        .addSpan(new StyleSpan(Color.GREEN)));
kikoso commented

Hi @mintheinwin ,

You are setting a single polyline. Could you try adding a polyline from A to B with a given color, and then another polyline from B to C with a different color?

Hi @kikoso , Yes I had tried like that. I have solve my problem.

public ArrayList list = new ArrayList();

getPermissionAndLocationChange(list);

    private void getPermissionAndLocationChange(final ArrayList<LatLng> tList) {
    locationCallback = new LocationCallback() {
        //This callback is where we get "streaming" location updates. We can check things like accuracy to determine whether
        //this latest update should replace our previous estimate.
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                Log.d(TAG, "locationResult null");
                return;
            }
            Log.d(TAG, "received " + locationResult.getLocations().size() + " locations");
            for (Location loc : locationResult.getLocations()) {
                tList.add(new LatLng(loc.getLatitude(), loc.getLongitude()));

                //StampStyle stampStyle = TextureStyle.newBuilder(BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow)).build();
                if (colorStart == 0) {
                    styleSpanArrayList.add(new StyleSpan(StrokeStyle.colorBuilder(Color.GREEN).build()));
                    colorStart = 1;
                } else if (colorStart == 1) {
                    styleSpanArrayList.add(new StyleSpan(StrokeStyle.colorBuilder(Color.YELLOW).build()));
                    colorStart = 2;
                } else if (colorStart == 2) {
                    styleSpanArrayList.add(new StyleSpan(StrokeStyle.colorBuilder(Color.RED).build()));
                    colorStart = 0;
                }
                if (line!=null)line.remove();
                line = map.addPolyline(new PolylineOptions().addAll(tList).width(30).addAllSpans(styleSpanArrayList));

               /* tList.add(new LatLng(1.4384168567466844, 103.80680969583258));
                styleSpanArrayList.add(new StyleSpan(StrokeStyle.colorBuilder(Color.GREEN).build()));
                tList.add(new LatLng(1.4393477524038727, 103.80260854186459));
                styleSpanArrayList.add(new StyleSpan(StrokeStyle.colorBuilder(Color.YELLOW).build()));
                tList.add(new LatLng(1.4408750600926357, 103.80097019583253));
                styleSpanArrayList.add(new StyleSpan(StrokeStyle.colorBuilder(Color.RED).build()));

                Polyline line = map.addPolyline(new PolylineOptions().addAll(tList).width(30).addAllSpans(styleSpanArrayList));*/

            }
        }

        @Override
        public void onLocationAvailability(LocationAvailability locationAvailability) {
            Log.d(TAG, "locationAvailability is " + locationAvailability.isLocationAvailable());
            super.onLocationAvailability(locationAvailability);
        }
    };

}
kikoso commented

Great to know! Thanks for letting us know, @mintheinwin