2013-05-09 179 views
0

Hello Stack Overflow people!使用JavaScript更改KML地標顏色

我真的不知道是否有辦法做到這一點,但我希望自從替代方案變得更加複雜,我不積極如何實現它。目前,我有一個Google Earth插件在其他控件的頁面上運行。該頁面應顯示調制解調器的延遲和信號噪聲數據圖表,以及包含大量附加信息的網格,以幫助我的ISP在解決調制解調器故障時做得更好。

我的問題是:JavaScript中是否有方法修改Google Earth地標的顏色而不妨礙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> 

但我一直有在JavaScript中使用C#或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,並試圖將其添加到不同的地方,大部分時間它只是打破了,並沒有顯示天氣數據或地標。最終目標是根據遙控器是標稱的,處於報警狀態還是處於警告狀態,更改爲地標的顏色。我已經在這裏和谷歌上尋找答案,但似乎沒有在JS中這樣做,我似乎無法以正確的方式追加KML來改變地標顏色。有人有任何想法嗎?

回答

0

我終於明白了我自己的想法。希望任何尋找答案的人都可以在這裏找到答案。

我看到很多建議根據樣式換出不同顏色的圖像,但找不到描述如何去做的任何地方。原因是JavaScript隱藏在無處不在的中間。 Google的API可能...有時並不十分用戶友好。所以,可搜索更好,對吧?我希望這會幫助別人。

在C#我有與在那裏我有存儲的圖像,並把它傳遞給JavaScript鏈接(絕對路徑,因爲當地人不想工作)到一個地方的switch語句:

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或其他顏色的飽和度,你很好。乾杯!