2011-11-01 82 views
3

我需要一個自定義的KML文件加載到谷歌地圖,代碼幾乎持平適當ressources:geoxml3:如何加載自定義圖標

function initialize() { 
var myLatlng = new google.maps.LatLng(39.397, -100.644); 
    var myOptions = { 
    zoom: 5, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 

    var geoXml = new geoXML3.parser({map: map, processStyles: true}); 
    geoXml.parse('test.kml'); 
}; 

的.KML來直出的谷歌地圖和有一堆標記,所有自定義圖標,例如:

<Style id="sn_1"> 
    <IconStyle> 
     <scale>1.1</scale> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/paddle/Z.png</href> 
     </Icon> 
     <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/> 
    </IconStyle> 
    <ListStyle> 
     <ItemIcon> 
      <href>http://maps.google.com/mapfiles/kml/paddle/Z-lv.png</href> 
     </ItemIcon> 
    </ListStyle> 
</Style> 

其它圖標被定義爲(現有的)本地路徑,如:

<Icon> 
<href>img/marker/5.png</href> 
</Icon> 

但是,雖然地圖顯示得很好,但沒有任何圖標會被加載,而是我只會獲取默認的Google地圖圖標。任何幫助,這將不勝感激,因爲我的JavaScript知識是相當有限的,我覺得我在一個點,更headscratching不會得到我在任何地方......

乾杯:)

回答

1

我解決使用國際電話區號:

var marker = new google.maps.Marker({position:point,icon:placemark.style.icon});

這個完整的初始化:由格里尼奧對我來說效果很好,但在KML文件中的所有標記提供

 //Construct a geoXML3 parser 
     var myParser = new geoXML3.parser({ 
       map: map, 
       singleInfoWindow:true, 
       processStyles: true, 
       createMarker:function(placemark){ 
        //Constructing marker for each Placemark node, and then add it to the markclustere 
        var point = new google.maps.LatLng(placemark.point.lat, placemark.point.lng); 
        var marker = new google.maps.Marker({position:point,icon: placemark.style.icon}); 

        google.maps.event.addListener(marker, "click", function(){ 
         //console.log(placemark.name); 
         infoWindow.content = placemark.description; 
         infoWindow.open(map, marker); 
        }); 

        markerclusterer.addMarker(marker); 
       } 
     }); 
2

回答必須有一個風格類似這樣或檢查 placemark.style.icon前P將未定義,功能:

<Document> 
<name>myKml.kml</name> 
<Style id="My_Style"> 
    <IconStyle> 
     <Icon> 
      <href>office-building.png</href> 
     </Icon> 
    </IconStyle> 
</Style> 

,並在標部分:

<Placemark> 
    <name><![CDATA[iMEX0011]]></name> 
    <description><![CDATA[MARKER TITLE]]></description> 
    <styleUrl>#My_Style</styleUrl> 
    <Point> 
     <coordinates>-99.2232472,25.4413972,0</coordinates> 
    </Point> 
</Placemark> 
+0

這非常有幫助。謝謝@ user3528606 –