2013-03-19 154 views
0

我正在使用geoxml客棧來解析包含點數的kml文件。每個標記都有一個包含一些信息的信息窗口。現在我想要在每個信息窗口中添加一個按鈕,並且可以在文本框中顯示該特定信息窗口中的信息。將按鈕添加到infoWindow

現在我的問題是我怎麼能夠添加這樣的按鈕,點擊我會得到infowindow的信息?

下面是一個信息窗口的圖像:

enter image description here

這是到目前爲止的代碼我做:

 function initialize() { 

       var mapOptions = { 
        center: new google 

.maps.LatLng(35.898737028438, 14.5133403246687), 
       zoom: 17, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

      infowindow = new google.maps.InfoWindow({}); 

     } 


     function displayKml() { 
      initialize(); 

      parser = new geoXML3.parser({ 
       map: map, 
       infoWindow: infowindow, 
       singleInfoWindow: true, 
       zoom: true, 
       markerOptions: { optimized: false } 
      }); 
      parser.parse("Uploads/" + document.getElementById('<%= text2.ClientID %>').value); 

     } 

KML文件

<?xml version="1.0" encoding="utf-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
    <Document> 
    <name>route</name> 
    <Placemark> 
     <name>188</name> 
     <description>museum</description> 
     <Point> 
     <coordinates>14.5104009086433,35.8994513796904</coordinates> 
     </Point> 
    </Placemark> 
    <Placemark> 
     <name>196</name> 
     <description>museum</description> 
     <Point> 
     <coordinates>14.5105859971021,35.8991906966932</coordinates> 
     </Point> 
    </Placemark> 
    <Placemark> 
     <name>349</name> 
     <description>museum</description> 
     <Point> 
     <coordinates>14.5126379237713,35.8969782492105</coordinates> 
     </Point> 
    </Placemark> 
    </Document> 
</kml> 
+1

geoxml3是非常靈活的,有幾個方法可以做到這一點,其中一個還說將HTML添加到您的KML(使用onclick中的頁面使用JavaScript函數)。另一個是重寫createMarker函數。你的KML是什麼樣的?這是你需要添加的最後一件事情(至少是映射代碼)?這僅用於標記還是用於多段線/多邊形? – geocodezip 2013-03-19 13:15:54

+0

實際上,它幾乎是映射代碼的最後一件事情。我用kml文件更新了這個問題。只有標記我打算這樣做。 @geocodezip – 2013-03-19 13:53:15

回答

2

一種方法:覆蓋createMarker函數:

function displayKml() { 
    geo = new geoXML3.parser({ 
     map: map, 
     zoom: true, 
     singleInfoWindow: true, 
     infoWindow: infowindow, 
     createMarker: createMarker 
    }); 
    geo.parse(document.getElementById('kmlFile').value); 
} 

function createMarker(placemark, doc) { 
    // create a Marker to the map from a placemark KML object 

    // Load basic marker properties 
    var markerOptions = { 
     map: map, 
     position: new google.maps.LatLng(placemark.Point.coordinates[0].lat, placemark.Point.coordinates[0].lng), 
     title: placemark.name, 
     zIndex: Math.round(placemark.Point.coordinates[0].lat * -100000)<<5, 
     icon:  placemark.style.icon, 
     shadow: placemark.style.shadow 
    }; 

    // Create the marker on the map 
    var marker = new google.maps.Marker(markerOptions); 
    if (!!doc) { 
     // doc.markers.push(marker); 
    } 

    // Set up and create the infowindow 
    var infoWindowOptions = { 
     content: '<div class="geoxml3_infowindow"><h3>' + placemark.name + 
       '</h3><div>' + placemark.description + '</div>'+ 
       '<input type="button" onclick="displayInfo(\''+placemark.name+'\',\''+placemark.description+'\');" value="populate div"></input>', 
     pixelOffset: new google.maps.Size(0, 2) 
    }; 
    infowindow.setOptions(infoWindowOptions); 
    marker.infoWindowOptions = infoWindowOptions; 
    marker.infoWindow = infowindow; 
    // Infowindow-opening event handler 
    google.maps.event.addListener(marker, 'click', function() { 
     this.infoWindow.close(); 
     marker.infoWindow.setOptions(this.infoWindowOptions); 
     this.infoWindow.open(this.map, this); 
    }); 
    placemark.marker = marker; 
    return marker; 
} 

添加一個函數來顯示在外部DIV數據:

function displayInfo(name,description){ 
    document.getElementById('info').innerHTML = name+"<br>"+description; 
} 

working example

+0

非常感謝,我真的很感激你的幫助。它工作出色。感謝您在我所有帖子和查詢中提供的所有幫助。 :) – 2013-03-19 14:53:07

+0

最好的感謝就是回頭接受以前任何有助於解決問題的答案(如果有用的話)。 – geocodezip 2013-03-19 17:04:46

+0

Dw我會做,如果有的話,我會將它們標記爲已接受的答案。謝謝 – 2013-03-19 17:28:16