我有一个项目,可以在 map 上绘制并生成 kml 作为字符串。我想通过 ajax 请求将其发送到 kml 文件。
$.ajax({
type: 'PUT',
url: 'myurl.kml',
data: kmlString,
success() {
//say it succeeded
}
});
因为我的 kml 数据嵌套在引号内,就像这样,
'<xml.....</kml>'
kml 文件提示由于引号而导致语法不正确(这是事实)。
我想过将数据作为 json 对象而不是字符串传递,就像这样,
{'mapData': 'mykmldata'}
但数据仍然用引号封装。
将我的数据获取到不带引号的 kml 文件的最佳方法是什么? Google map API v3 似乎在任何地方都没有“字符串到 kml”功能,尽管我觉得这在这种情况下是万能的。
我注意到 Google Earth API 有一个 parseKml 函数,它与谷歌地图兼容吗?
最佳答案
第三方 KML 解析器 geoxml3有一个“parseKmlString”方法,该方法将从字符串中获取有效的 KML 并解析它以创建 native Google Maps Javascript API v3 对象。
示例: 从此文件中获取 KML: http://www.geocodezip.com/geoxml3_test/kml/MA_cities.kml
片段:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Massachusetts Cities</name>
<Folder>
<Placemark>
<name>Boston</name>
<description>Boston is the capital of and largest city in Massachusetts. The Boston Massacre and the Boston Tea Party occurred in Boston and led to the American Revolution.</description>
<LookAt>
<longitude>-71.05977300312775</longitude>
<latitude>42.35843100531216</latitude>
<altitude>0</altitude>
<heading>-2.107386233340164e-009</heading>
<tilt>0</tilt>
<range>34426.00143998101</range>
<altitudeMode>relativeToGround</altitudeMode>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin9</styleUrl>
<Point>
<altitudeMode>absolute</altitudeMode>
<coordinates>-71.05977300312775,42.35843100531217,3.1482280535562</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Worcester</name>
<description>Worcester is known as the "Heart of the Commonwealth" due to its location in central Massachusetts, thus, a heart is the official symbol of the city.</description>
<LookAt>
<longitude>-71.80229299737233</longitude>
<latitude>42.2625930065606</latitude>
<altitude>0</altitude>
<heading>1.76716070878667e-009</heading>
<tilt>0</tilt>
<range>17233.50055269895</range>
<altitudeMode>relativeToGround</altitudeMode>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin0</styleUrl>
<Point>
<altitudeMode>absolute</altitudeMode>
<coordinates>-71.80229299737233,42.26259300656061,145.2545892926215</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
将其粘贴到本页的 KML 文本框中:
http://www.geocodezip.com/blitz-gmap-editor/test5.html
它显示来自 KML 的标记。
关于javascript - 写入kml文件: How send kml string to kml file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19913837/