简体   繁体   English

导出KML谷歌地图

[英]export KML google map

I use google map APIv3, I create my map with different marker and I create an itinerary everything works. 我使用Google Map APIv3,使用其他标记创建地图,并创建行程表,一切正常。 My question is: I want export my itinerary in KML file with javascript, I have make lot of serch but no good answer or tuto found, anyone have solution or export with javascript isn't possible? 我的问题是:我想使用javascript将行程导出到KML文件中,我进行了大量搜索,但是找不到好的答案或tuto,没有人有解决方案或无法使用javascript导出吗? Maybe alternative solution is possible... 也许替代解决方案是可能的...

This is not a solution, and a bit half-assed. 这不是一个解决方案,有点犹豫不决。 A year old nodejs-project I dug up. 我挖了一个岁的nodejs-project。 Maybe it can give you a few ideas. 也许可以给您一些想法。

Yeah, I realize you want a browser solution. 是的,我知道您需要浏览器解决方案。 (Sorry!) (抱歉!)

Browser javascript doesn't work with files very well. 浏览器javascript不能很好地处理文件。 But you absolutely can use data URI to make the data downloadable, or you can fill a textarea and let the user copy the KML by hand. 但是您绝对可以使用数据URI来使数据可下载,或者您可以填充文本区域并让用户手动复制KML。

The actual KML is a simple xml-file, and you can generate it any way you see fit. 实际的KML是一个简单的xml文件,您可以按照自己认为合适的任何方式生成它。

var kml = "<?xml version="1.0" encoding="UTF-8"?><kml ...";  // done?

You can use templates to make things easier. 您可以使用模板使事情变得简单。

Here is my server solution. 这是我的服务器解决方案。 It's pretty light weight, taking geotweets (json) for input and spitting out kml-files. 它的重量很轻,采用geotweets(json)作为输入并吐出kml文件。 I was using EJS for templating. 我使用EJS进行模板制作。 I know EJS runs in the browser as well, so perhaps the following can help you a little at least. 我知道EJS也可以在浏览器中运行,因此以下内容至少可以对您有所帮助。

My template file (This was for google earth, and probably don't work in google maps because of the LatLonQuad, and lines with altitude. But other than that I know gmaps kml are very, very similar to google earth kml, and more often than not it's cross-compatible): 我的模板文件(这是用于Google Earth的文件,由于LatLonQuad以及带有高度的线条,可能无法在Google Maps中使用。但是除此之外,我知道gmaps kml与Google Earth 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">
    <Document>
        <Style id="whiteLine">
            <LineStyle>
                <color>ffffffff</color>
                <width>1</width>
            </LineStyle>
        </Style>
        <Placemark id="<%= id %>">
            <styleUrl>#whiteLine</styleUrl>
            <MultiGeometry>
                <LineString>
                    <altitudeMode>absolute</altitudeMode>
                    <coordinates>
                        <%= lineStart %>
                        <%= lineEnd %>
                    </coordinates>
                </LineString>
                <Polygon id="<%= id %>_poly">
                    <altitudeMode>absolute</altitudeMode>
                    <outerBoundaryIs>
                        <LinearRing>
                            <coordinates>
                                <%= quadP1 %>,<%= polyAltitude %>
                                <%= quadP2 %>,<%= polyAltitude %>
                                <%= quadP3 %>,<%= polyAltitude %>
                                <%= quadP4 %>,<%= polyAltitude %>
                                <%= quadP1 %>,<%= polyAltitude %>
                            </coordinates>
                        </LinearRing>
                    </outerBoundaryIs>
                </Polygon>
            </MultiGeometry>
        </Placemark>
        <GroundOverlay>
            <Icon>
                <href><%= image %></href>
                <viewBoundScale>0.75</viewBoundScale>
            </Icon>
            <altitudeMode>absolute</altitudeMode>
            <altitude><%= quadAltitude %></altitude>
            <gx:LatLonQuad>
                <coordinates>
                    <%= quadP1 %>
                    <%= quadP2 %>
                    <%= quadP3 %>
                    <%= quadP4 %>
                </coordinates>
            </gx:LatLonQuad>
        </GroundOverlay>
    </Document>
</kml>

And I feeded it data like this. 然后我像这样输入数据。

exports.generate = function (tweet, config, callback) {
    var ejs = require('ejs');
    var fs = require('fs');

    // ... Omitted stuff for brewity

    fs.readFile(config.kmlTemplate, 'utf8', function (err, template) {
        var content = ejs.render(template, {
            id           : tweet.id_str,
            lineStart    : geo[1].toString() + ',' + geo[0].toString() + ',' + altitude,
            lineEnd      : geo[1].toString() + ',' + geo[0].toString() + ',0',
            image        : config.imageUrl + tweet.id_str + '.png',
            quadAltitude : altitude,
            polyAltitude : altitude - 1,
            quadP1       : ( geo[1] - width ).toString() + ',' + ( geo[0] - height ).toString(),
            quadP2       : ( geo[1] + width ).toString() + ',' + ( geo[0] - height ).toString(),
            quadP3       : ( geo[1] + width ).toString() + ',' + ( geo[0] + height ).toString(),
            quadP4       : ( geo[1] - width ).toString() + ',' + ( geo[0] + height ).toString()
        });

        fs.writeFile(config.kmlPath + tweet.id_str + '.kml', content, function (err) {
            if (err) { console.log(err); }
            else { callback(); }
        });
    });

Also google's KML reference is all you really need, but it's a nightmare to go through. 谷歌的KML参考资料也是您真正需要的,但这是一场噩梦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM