fulgen
fulgen

Reputation: 191

Change the color of polyline in android google map v2

I have this code found from this link https://developers.google.com/maps/documentation/android/shapes#customizing_appearances

Polyline line = map.addPolyline(new PolylineOptions()
    .add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734))
    .width(25)
    .color(Color.BLUE)
    .geodesic(true));

My Problem is Color in Color.Blue returns an error saying The name Color does not exist in the current context.

Upvotes: 5

Views: 22870

Answers (9)

Kyle
Kyle

Reputation: 1170

The right answer as of Android 6.0 is to use R.color directly:

PolylineOptions rectLine = new PolylineOptions().width(4).color(R.color.cyan);

Upvotes: 0

Shrawan Thakur
Shrawan Thakur

Reputation: 869

Try this:

PolylineOptions lineOptions = new PolylineOptions();
lineOptions.addAll(points);
lineOptions.width(12);
lineOptions.color(Color.BLUE); //this is for color
lineOptions.geodesic(true);

Upvotes: 4

What worked for me was just this:

                      lineoptions.color(getResources().getColor(R.color.colorP));

Where colorP is:

<?xml version="1.0" encoding="utf-8"?><resources><color name="colorP">#ff8100</color></resources>

in color file

Upvotes: 1

Latief Anwar
Latief Anwar

Reputation: 1868

Try this, work for me.

lineOptions = new PolylineOptions();
lineOptions.color(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary));

Upvotes: 3

Jordi P&#233;rez
Jordi P&#233;rez

Reputation: 11

Another working solution is:

import android.graphics.Color;

and then use:

Color.BLUE

to represent the blue color like you did the fist time. No need to create the Color.xml file.

Upvotes: 1

fulgen
fulgen

Reputation: 191

I already solved my problem.

First I created an xml file in my values folder Color.xml

Color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#0000EE</color>
</resources>

Then in my Activity

Polyline line = map.addPolyline(new PolylineOptions()
    .Add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734))
    .InvokeColor(Resources.GetColor(Resource.Color.blue));

Upvotes: 4

Namrata
Namrata

Reputation: 1684

Try this:

PolylineOptions polyline_options = new PolylineOptions()
                        .addAll(arraylist_lat_lon).color(Color.GREEN).width(2);

                polyline = googleMap.addPolyline(polyline_options);

Before this, add color xml to you values folder, and define all colors what you want. Just like this manner:-

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <color name="blue">#82CAFF</color>
    <color name="red">#FF0000</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="symptom_color_selected">#003366</color>
    <color name="reaction_color_selected">#003366</color>
    <color name="static_text_color">#003366</color>
    <color name="journal_entry_listitem_text_color">#003366</color>
    <color name="note_text_disabled">#333333</color>
    <color name="blue">#0000FF</color>
    <color name="navy">#6699FF</color>
    <color name="sky">#0099CC</color>
    <color name="gray">#808080</color>
    <color name="lightgray">#e7e7e7</color>
    <color name="lightgray02">#bfbfbf</color>
    <color name="dark">#000015</color>
    <color name="lightgreen">#336666</color>
    <color name="orrange">#e33d1b</color>
    <color name="darkorrange">#cc2303</color>
    <color name="pressed_color">#FF8E4067</color>
    <color name="focussed_color">#DD8E4067</color>

</resources>

Upvotes: 2

M D
M D

Reputation: 47807

Instead of creating too many short Polylines just create one like here:

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);

Here, sets geodesic(true) whether to draw each segment of the line as a geodesic or not.

Upvotes: 1

Emil Adz
Emil Adz

Reputation: 41099

Just define a color in your colors resources file and do this:

PolylineOptions rectLine = new PolylineOptions().width(4).color(context.getResources().getColor(R.color.cyan));

Works great for me.

Upvotes: 10

Related Questions