使用JavaScript更改KML地标颜色

时间:2013-05-09 17:12:36

标签: javascript asp.net kml

Hello Stack Overflow人!!

老实说,我不知道是否有办法做到这一点,但我希望有,因为替代方案更复杂,我不肯定如何实现它。目前,我已经在一个带有其他控件的页面上运行了一个Google Earth插件。该页面应该显示一个图表,其中包含调制解调器的延迟和信噪比数据,以及一个包含大量附加信息的网格,以帮助我的ISP在解决问题调制解调器方面做得更好。

我的问题是:JavaScript是否有办法修改Google地球地标的颜色而不会弄乱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>

找到将上面的KML添加到后面的C​​#代码中的字符串并找到正确添加样式的地方会更好吗?我一直在倾注谷歌的API,并试图在不同的地方添加它,大多数时候它只是打破,并没有显示天气数据或地标。最终目标是根据遥控器是标称,警报还是警告状态更改为地标的颜色。我在这里和谷歌上寻找答案,但似乎没有任何东西在JS中做到这一点我似乎无法以正确的方式追加KML来改变地标颜色。有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我最终自己想出了这个。希望任何寻找答案的人都能在这里找到答案。

我看到很多东西建议根据样式换出不同颜色的图像,但找不到描述如何操作的任何地方。原因是JavaScript隐藏在不知名的中间。谷歌的API有时候......用户不太友好。所以,可搜索的更好,对吧?我希望这会帮助别人。

在C#中,我有一个带有链接的switch语句(因为本地人不想工作的绝对路径)到我已经存储了图像并将其传递给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中的颜色饱和度,你就可以了。干杯!

相关问题