3

我正在使用 google maps javascript api 根据用户输入制作多边形(我没有使用绘图库,只是直接使用 v3)。我希望能够将这些功能导出到 kml。但是,我在 javascript api 中看不到任何允许我执行此操作的内容。

我知道地图引擎精简版具有“导出到 kml”功能。

我是否缺少一些允许导出到 kml 的谷歌库?我见过一些解决方案,其中多边形的属性被发送到文本区域并用于生成 kml,但我更喜欢使用现有的库而不是制作自己的 kml 文件。

4

1 回答 1

6

Blitz GMap Editor允许您导出 KML(使用绘图管理器创建和编辑它们,但您可以使用您的代码)。

例子

Blitz GMap Editor到 KML 代码。取决于xmlwriter.js

this.toKML = function() {
  var result = mapToObject();
  var xw = new XMLWriter('UTF-8');
  xw.formatting = 'indented'; //add indentation and newlines
  xw.indentChar = ' '; //indent with spaces
  xw.indentation = 2; //add 2 spaces per level

  xw.writeStartDocument();
  xw.writeStartElement('kml');
  xw.writeAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
  xw.writeStartElement('Document');

  for (var i = 0; i < result.overlays.length; i++) {
    xw.writeStartElement('Placemark');
    xw.writeStartElement('name');
    xw.writeCDATA(result.overlays[i].title);
    xw.writeEndElement();
    xw.writeStartElement('description');
    xw.writeCDATA(result.overlays[i].content);
    xw.writeEndElement();
    if (result.overlays[i].type == "marker") {

      xw.writeStartElement('Point');
      xw.writeElementString('extrude', '1');
      xw.writeElementString('altitudeMode', 'relativeToGround');
      xw.writeElementString('coordinates', result.overlays[i].position.lng.toString() + "," + result.overlays[i].position.lat.toString() + ",0");
      xw.writeEndElement();

    } else if (result.overlays[i].type == "polygon" || result.overlays[i].type == "rectangle" || result.overlays[i].type == "circle") {
      xw.writeStartElement('Polygon');
      xw.writeElementString('extrude', '1');
      xw.writeElementString('altitudeMode', 'relativeToGround');

      if (result.overlays[i].type == "rectangle") {
        //its a polygon
        xw.writeStartElement('outerBoundaryIs');
        xw.writeStartElement('LinearRing');
        xw.writeStartElement("coordinates");
        xw.writeString(result.overlays[i].bounds.sw.lng + "," + result.overlays[i].bounds.sw.lat + ",0");
        xw.writeString(result.overlays[i].bounds.ne.lng + "," + result.overlays[i].bounds.sw.lat + ",0");
        xw.writeString(result.overlays[i].bounds.ne.lng + "," + result.overlays[i].bounds.ne.lat + ",0");
        xw.writeString(result.overlays[i].bounds.sw.lng + "," + result.overlays[i].bounds.ne.lat + ",0");
        xw.writeEndElement();
        xw.writeEndElement();
        xw.writeEndElement();
      }
      if (result.overlays[i].type == "circle") {
        //its a polygon, approximate a circle by a circular 64 sided polygon.
        xw.writeStartElement('outerBoundaryIs');
        xw.writeStartElement('LinearRing');
        xw.writeStartElement("coordinates");
        var d2r = Math.PI / 180; // degrees to radians 
        var r2d = 180 / Math.PI; // radians to degrees 
        var earthsradius = 6378137; // 6378137 is the radius of the earth in meters
        var dir = 1; // clockwise

        var points = 64;

        // find the raidus in lat/lon 
        var rlat = (result.overlays[i].radius / earthsradius) * r2d;
        var rlng = rlat / Math.cos(result.overlays[i].center.lat * d2r);

        var extp = new Array();
        if (dir == 1) {
          var start = 0;
          var end = points + 1
        } // one extra here makes sure we connect the line
        else {
          var start = points + 1;
          var end = 0
        }
        for (var j = start;
          (dir == 1 ? j < end : j > end); j = j + dir) {
          var theta = Math.PI * (j / (points / 2));
          ey = result.overlays[i].center.lng + (rlng * Math.cos(theta)); // center a + radius x * cos(theta) 
          ex = result.overlays[i].center.lat + (rlat * Math.sin(theta)); // center b + radius y * sin(theta) 
          xw.writeString(ey + "," + ex + ",0");
        }
        xw.writeEndElement();
        xw.writeEndElement();
        xw.writeEndElement();
      } else {
        for (var j = 0; j < result.overlays[i].paths.length; j++) {
          if (j == 0) {
            xw.writeStartElement('outerBoundaryIs');
          } else {
            xw.writeStartElement('innerBoundaryIs');
          }
          xw.writeStartElement('LinearRing');
          xw.writeStartElement("coordinates");
          for (var k = 0; k < result.overlays[i].paths[j].length; k++) {
            xw.writeString(result.overlays[i].paths[j][k].lng + "," + result.overlays[i].paths[j][k].lat + ",0");
          }
          xw.writeEndElement();
          xw.writeEndElement();
          xw.writeEndElement();
        }
      }
      xw.writeEndElement();

    } else if (result.overlays[i].type == "polyline") {
      xw.writeStartElement('LineString');
      xw.writeElementString('extrude', '1');
      xw.writeElementString('altitudeMode', 'relativeToGround');
      xw.writeStartElement("coordinates");
      for (var j = 0; j < result.overlays[i].path.length; j++) {
        xw.writeString(result.overlays[i].path[j].lng + "," + result.overlays[i].path[j].lat + ",0");
      }
      xw.writeEndElement();
      xw.writeEndElement();

    }

    xw.writeEndElement(); // END PlaceMarker
  }

  xw.writeEndElement();
  xw.writeEndElement();
  xw.writeEndDocument();

  var xml = xw.flush(); //generate the xml string
  xw.close(); //clean the writer
  xw = undefined; //don't let visitors use it, it's closed
  //set the xml
  document.getElementById('kmlString').value = xml;
}
于 2013-11-05T16:49:51.313 回答