Change the color of polyline in android google map v2

ぃ、小莉子 提交于 2019-12-07 09:57:04

问题


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.


回答1:


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.




回答2:


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



回答3:


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>



回答4:


Try this:

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



回答5:


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.




回答6:


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.




回答7:


Try this, work for me.

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



回答8:


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




回答9:


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

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


来源:https://stackoverflow.com/questions/21592789/change-the-color-of-polyline-in-android-google-map-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!