Dynamically change the color of the KML polygon in the Google Maps API v3

I am using the Google Maps API v3 to load a KML layer and want to be able to change the default KML color by default without editing the KML file itself. Is this possible using JavaScript or any other means?

Thanks Neil

+3
source share
2 answers

From my understanding of the documentation, “no,” but this is not particularly clear. I am trying to do a similar thing (but updating the color of mouseover / mouseout).

The KML file is uploaded by Google servers, parsed and sent to your javascript object for application to the map, so by the time your javascript KMLLayer sees it, it’s all figured out.

, - Url. , , .

+2

, -, KML , KML, URL . - .net , -.

, , :

    public string KMLStyler(string URL, string URLName, Data[] MyData)
    {
        try
        {
            ReadFile(URL);

            string NewKML = ReadFile(URL);

            string RedStyle = "<Style id=\"red\"><LineStyle><color>7F7F7F7F</color><width>2</width></LineStyle><PolyStyle><color>7F0000FF</color><fill>1</fill><outline>1</outline></PolyStyle></Style>";
            string BlackStyle = "<Style id=\"black\"><LineStyle><color>7F7F7F7F</color><width>2</width></LineStyle><PolyStyle><color>7F7F7F7F</color><fill>1</fill><outline>1</outline></PolyStyle></Style>";
            string GreenStyle = "<Style id=\"green\"><LineStyle><color>7F7F7F7F</color><width>2</width></LineStyle><PolyStyle><color>7F00FF00</color><fill>1</fill><outline>1</outline></PolyStyle></Style>";
            string BlueStyle = "<Style id=\"blue\"><LineStyle><color>7F7F7F7F</color><width>2</width></LineStyle><PolyStyle><color>7F7F7F7F</color><fill>1</fill><outline>1</outline></PolyStyle></Style>";


            //add styles to top
            int EndID = 0;
            EndID = NewKML.IndexOf("</name>") + 7;
            NewKML = NewKML.Insert(EndID, RedStyle);

            EndID = NewKML.IndexOf("</name>") + 7;
            NewKML = NewKML.Insert(EndID, BlackStyle);

            EndID = NewKML.IndexOf("</name>") + 7;
            NewKML = NewKML.Insert(EndID, GreenStyle);

            EndID = NewKML.IndexOf("</name>") + 7;
            NewKML = NewKML.Insert(EndID, BlueStyle);

            //add each style to each placemark


            foreach (Data MyDataSingle in MyData)
            {
                int NamePos = NewKML.IndexOf(MyDataSingle.Name);

                if (NamePos == -1) throw new Exception("Did not find '" + MyDataSingle.Name + "' within File");
                NamePos += MyDataSingle.Name.Length + 7;

                int MultiGeometryStartPos = NewKML.IndexOf("<MultiGeometry>", NamePos);
                int MultiGeometryEndPos = NewKML.IndexOf("</MultiGeometry>", NamePos);
                int PolygonStartPos = NewKML.IndexOf("<Polygon>", NamePos);

                int InsertPos = 0;
                if (MultiGeometryStartPos < PolygonStartPos)
                {
                    if (MultiGeometryStartPos != -1)
                    {
                        InsertPos = MultiGeometryStartPos;
                    }
                    else
                    {
                        InsertPos = PolygonStartPos;
                    }
                }
                else
                {
                    InsertPos = PolygonStartPos;
                }

                if (MyDataSingle.Red)
                {
                    NewKML = NewKML.Insert(InsertPos, "<styleUrl>#red</styleUrl>");
                }
                if (MyDataSingle.Black)
                {
                    NewKML = NewKML.Insert(InsertPos, "<styleUrl>#black</styleUrl>");
                }
                if (MyDataSingle.Green)
                {
                    NewKML = NewKML.Insert(InsertPos, "<styleUrl>#green</styleUrl>");
                }
                if (MyDataSingle.Blue)
                {
                    NewKML = NewKML.Insert(InsertPos, "<styleUrl>#blue</styleUrl>");
                }
            }

            string NewFileName = WriteFile(NewKML, URLName);

            return NewFileName;
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

    public string WriteFile(string KMLData, string URLName)
    {
        string FileName = "http:\\blah.co.uk\blah.kml";
        StreamWriter writer = new StreamWriter("C:/inetpub/blah.kml");

        writer.Write(KMLData);
        writer.Flush();
        writer.Close();

        return FileName;
    }

    public string ReadFile(string URL)
    {
        string File = "";
        StreamReader reader = new StreamReader(WebRequest.Create(URL).GetResponse().GetResponseStream());
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            File += line;
        }

        return File;
    }
+1

All Articles