javascript - Draw kml layer on google map from a text area -


i'd draw kml layer on google map javascript api having kml code copied in textarea, in page:

http://display-kml.appspot.com/

all examples found in documentation load kml layer file;

example. uses geoxml3 third party kml parser , third party library adds ability edit kml (which doesn't sound need).

working fiddle

code snippet:

var geocoder;  var map;    function initialize() {    map = new google.maps.map(      document.getelementbyid("map_canvas"), {        center: new google.maps.latlng(37.4419, -122.1419),        zoom: 13,        maptypeid: google.maps.maptypeid.roadmap      });    document.getelementbyid("kmlstring").value = '<kml><document><placemark id="tester"><styleurl>#transyellowpolyactive</styleurl><name>tester</name><polygon><outerboundaryis><linearring><tessellate>0</tessellate><coordinates>6.30889892578125,53.28820543193896 6.29791259765625,53.28410053532493 6.233367919921875,53.21096737507053 6.5093994140625,53.19698389904798 6.50390625,53.27096221595853</coordinates></linearring></outerboundaryis></polygon></placemark><placemark id="jahoor"><styleurl>#transyellowpolyactive</styleurl><name>jahoor</name><polygon><outerboundaryis><linearring><tessellate>0</tessellate><coordinates>6.168479919433594,53.30318495818702 6.143760681152344,53.29579845109269 6.138267517089844,53.27281003615208 6.195259094238281,53.2707568976735 6.2010955810546875,53.29867113343524</coordinates></linearring></outerboundaryis></polygon></placemark></document></kml>';  }  google.maps.event.adddomlistener(window, "load", initialize);    var mapoverlays = [];  var geoxml = null;  //create common infowindow object  var infwindow = new google.maps.infowindow();    function setmapfromkml(kmlstring) {    if (kmlstring.length == 0) {      return false;    }    if (typeof geoxml3 == "undefined") { // check include of geoxml3 parser      // http://code.google.com/p/geoxml3/       alert("geoxml3.js not included");      return;    }    if (!geoxml)      geoxml = new geoxml3.parser({        map: map,        zoom: false,        suppressinfowindows: true      });      geoxml.parsekmlstring(kmlstring);      var tmpoverlay, ovroptions;    (var m = 0; m < geoxml.docs[0].placemarks.length; m++) {      if (geoxml.docs[0].placemarks[m].polygon) {          tmpoverlay = geoxml.docs[0].placemarks[m].polygon;        tmpoverlay.type = "polygon";      } else if (geoxml.docs[0].placemarks[m].linestring) {          tmpoverlay = geoxml.docs[0].placemarks[m].polyline;        tmpoverlay.type = "polyline";      } else if (geoxml.docs[0].placemarks[m].point) {          tmpoverlay = geoxml.docs[0].placemarks[m].marker;        tmpoverlay.type = "marker";      }        if (geoxml.docs[0].placemarks[m].name) {        tmpoverlay.title = geoxml.docs[0].placemarks[m].name;      } else {        tmpoverlay.title = "";      }        if (geoxml.docs[0].placemarks[m].description) {        tmpoverlay.content = geoxml.docs[0].placemarks[m].description;      } else {        tmpoverlay.content = "";      }        //attach click listener overlay      attachclicklistener(tmpoverlay);        //save overlay in array      mapoverlays.push(tmpoverlay);    }    map.fitbounds(geoxml.docs[0].bounds);  }    function attachclicklistener(overlay) {    google.maps.event.addlistener(overlay, "click", function(clkevent) {      var infcontent = getcontent(overlay);      openinfowindow(overlay, clkevent.latlng, infcontent);    });  }    function getcontent(overlay) {    var content = '<div><h3>' + overlay.title + '</h3>' + overlay.content + '<br></div>';    return content;  }    function openinfowindow(overlay, latlng, content) {    var div = document.createelement('div');    div.innerhtml = content;    setstyle(div, {      height: "100%"    });    infwindow.setcontent(div);    infwindow.setposition(latlng);    infwindow.relatedoverlay = overlay;    var t = overlay.get('fillcolor');    infwindow.open(mapobj);  }
html,  body,  #map_canvas {    height: 500px;    width: 500px;    margin: 0px;    padding: 0px  }
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=drawing,geometry"></script>  <script src="https://cdn.rawgit.com/geocodezip/geoxml3/master/polys/geoxml3.js"></script>  <script src="https://cdn.rawgit.com/geocodezip/geoxml3/master/projectedoverlay.js"></script>    <div id="map_canvas" style="border: 2px solid #3872ac;"></div>  <input type="button" value="parse kml map" onclick="setmapfromkml(document.getelementbyid('kmlstring').value)" />  <textarea id="kmlstring" style="width:100%; height:500px"></textarea>


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -