用JavaScript改变KML Placemark的颜色



大家好!

老实说,我不知道是否有办法做到这一点,但我希望有,因为替代方案更复杂,我不确定如何实现它。目前,我有一个谷歌地球插件运行与一些其他控件的页面。该页面应该显示一个图表,其中包含调制解调器的延迟和信噪比数据,以及一个网格,其中包含大量额外的信息,以帮助我的ISP在解决调制解调器故障时做得更好。

我的问题是:有没有一种方法在JavaScript中修改谷歌地球位置标记的颜色,而不搞乱KML?

我知道你可以在KML

中做这样的事情
<Style id="normalPlacemark">
  <IconStyle>
    <color>ffffff00</color>
    <scale>5</scale>
    <Icon>
      <href>http://maps.google.com/mapfiles/kml/pushpin/wht-pushpin.png</href>
    </Icon>
  </IconStyle>
</Style>

但是我一直在使用c#或JavaScript中的AddKMLFromString()将其附加到整个XML中的正确位置(我真的不熟悉)以使页面识别它。

这是修改后的插件代码,我正在使用的时刻:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    var ge;
    google.load("earth", "1");
    //Create an instance of the Earth
    function init() {
        google.earth.createInstance('gmap', initCallback, failureCallback);
    }
    function initCallback(pluginInstance) {
        ge = pluginInstance;
        ge.getWindow().setVisibility(true);
        //URL for KML file which is taken directly from Google
        //The KML in question is Google's giant file for weather data
        var href = 'aurlwherewetherdatalives.kml';
        //Use Google's fetch KML method to get the weather KML file and add it to our plugin's instance
        google.earth.fetchKml(ge, href, function (kmlObject) {
            if (kmlObject) {
                ge.getFeatures().appendChild(kmlObject);
            }
            if (kmlObject.getAbstractView() !== null)
                ge.getView().setAbstractView(kmlObject.getAbstractView());
        });
        //Turn on Country Borders, States, and Cities
        ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
        //By default, remoteExists uses True where JavaScript wants true
        var jsRemoteExists = <%= remoteExists.ToString().ToLower() %>;
        //If the remote exists, create a placemark and camera at its location using the
        //the latitude and longitude variables retreived in the c#
        if (jsRemoteExists)
            {
                //Variables have been created
                var lat = <%= CSHARPLat %>;
                var lon = <%= CSHARPLong %>;

                ge.getWindow().setVisibility(true);
                // Create the placemark and add it to Earth.
                var placemark = ge.createPlacemark('');
                // Set the placemark's location.  
                var point = ge.createPoint('');
                point.setLatitude(lat);
                point.setLongitude(lon);
                placemark.setGeometry(point);
                // Add the placemark to Earth.
                ge.getFeatures().appendChild(placemark);
                var la = ge.createLookAt('');
                la.setLatitude(lat);
                la.setLongitude(lon);
                la.setRange(150000);
                ge.getView().setAbstractView(la);
        }
    }
    function failureCallback(errorCode) {
    }
    function addKmlFromString(kmlString) {
        var kmlObject = ge.parseKml(kmlString);
        ge.getFeatures().appendChild(kmlObject);
    }

    window.onload = init();
</script>
在c#代码后面找到添加上述KML到字符串并找到添加样式的正确位置会更好吗?我一直在研究谷歌的API,并试图在不同的地方添加它,但大多数时候它只是坏了,不显示天气数据或placemark。最终目标是根据遥控器是标称状态、报警状态还是警告状态来改变placemark的颜色。我在这里和谷歌上寻找答案,但似乎没有在JS中做到这一点,我似乎无法以正确的方式添加KML来改变位置标记的颜色。有人有什么想法吗?

我最终自己弄明白了。希望,任何寻找答案的人都能在这里找到它。

我看到很多东西建议根据风格将图像换成不同的颜色,但找不到任何描述如何做的地方。原因是JavaScript隐藏在偏僻的地方。谷歌的API可以是……有时用户界面不是很友好。所以,可搜索的更好,对吧?我希望这能帮助到一些人。

在c#中,我有一个带有链接的开关语句(绝对路径,因为当地人不想工作)到我存储图像的地方,并将其传递给JavaScript:

var statusURL = '<%= CSHARPstatusURL %>';

稍后,您可能需要调整位置,这样您就不会隐藏任何其他图层,因为我是动态生成KML的,您需要这些方便的小行:

        // Define a custom icon.
        var icon = ge.createIcon('');
        icon.setHref(statusURL);
        var style = ge.createStyle(''); //create a new style
        style.getIconStyle().setIcon(icon); //apply the icon to the style
        style.getIconStyle().setScale(1.5); //set the icon's size
        placemark.setStyleSelector(style); //apply the style to the placemark

这将允许您创建图标样式,这是我一直希望的原始方式。自己创建一些图像,并在GIMP中修改颜色饱和度,这样就可以了。干杯!

最新更新