在Google Maps API v3中动态更改KML多边形的颜色

时间:2011-05-13 13:03:13

标签: google-maps-api-3 kml

我正在使用Google Maps v3 API来加载KML图层,并且希望能够从默认的蓝色更改KML的颜色,而无需编辑KML文件本身。这可能是使用JavaScript或其他方式吗?

谢谢, 尼尔

2 个答案:

答案 0 :(得分:2)

根据我对文档的理解,'不',但不是特别清楚。我正在尝试做类似的事情(但更新mouseover / mouseout的颜色)。

KML文件由Google服务器加载,解析并发送到您的javascript对象以应用于地图,因此,当您的javascript KMLLayer看到它时,它就全部整理好了。

您可以使用Styles和styleUrl执行某些操作。这应该允许你设置一些可以在运行时应用的不同样式,但是,我没有使用它。

答案 1 :(得分:1)

我通过创建一个Web服务来完成此操作,该服务将KML文件读入字符串,将样式部分插入KML字符串的开头,并将styleURL插入每个唯一命名的地标。使用.net Web服务修改标记并将其写回托管Web服务的服务器非常简单。

例如,它使用包含地标ID和颜色标记的类:

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