2

我有一个在地图上绘制并将 kml 生成为字符串的项目。我想通过 ajax 请求将其发送到 kml 文件。

$.ajax({
    type: 'PUT',
    url: 'myurl.kml',
    data: kmlString,
    success() {
       //say it succeeded
    }
});

因为我的 kml 数据嵌套在引号内,就像这样,

'<xml.....</kml>'

kml 文件抱怨由于引号导致语法不正确(这是真的)。

我想过将数据作为 json 对象而不是字符串传递,就像这样,

{'mapData': 'mykmldata'}

但数据仍然用引号封装。

将我的数据放入不带引号的 kml 文件的最佳方法是什么?Google maps API v3 似乎在任何地方都没有“string to kml”功能,尽管我觉得在这种情况下这将是灵丹妙药。

我注意到谷歌地球 API 有一个 parseKml 函数,它与谷歌地图兼容吗?

4

1 回答 1

2

第三方 KML 解析器geoxml3有一个“parseKmlString”方法,该方法将从字符串中获取有效的 KML 并对其进行解析以创建原生 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 &quot;Heart of the Commonwealth&quot; 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 的标记。

于 2013-11-11T19:25:09.103 回答