Google Maps API 和 KML 文件 LocalHost 开发选项 [英] Google Maps API and KML File LocalHost Development Options

查看:30
本文介绍了Google Maps API 和 KML 文件 LocalHost 开发选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Maps JavaScript version 3 API 图书馆文档清楚地解释了:

The Google Maps JavaScript version 3 API library documentation clearly explains:

Google Maps API 支持 KML和 GeoRSS 数据格式用于显示地理信息.这些数据格式显示在地图上使用KmlLayer 对象,其构造函数获取可公开访问的 URLKML 或 GeoRSS 文件.

The Google Maps API supports the KML and GeoRSS data formats for displaying geographic information. These data formats are displayed on a map using a KmlLayer object, whose constructor takes the URL of a publicly accessible KML or GeoRSS file.

甚至还有几个关于如何加载本地数据的 Stack Overflow 问题:

There are even several Stack Overflow questions about how to load local data:

一些答案​​指向第三方库,它们可以在本地解析 KML 而无需公开文件:

Some of the answers have pointed to third party libraries which can parse KML locally without the file needing to be public:

虽然如果您需要保护数据的私密性,这些解决方案很好,但我只是想让开发更容易.在本地运行时,我显然无法解析我的 KML,因此失去了我试图测试的功能.我已经在一个公开网站上发布了一个通用 KML 文件,但是在真正运行时必须使用不同的开发代码来渲染一个东西和其他东西.

And while these solutions are good if you have a need to keep your data private, I simply want to make development easier. When running locally I obviously cannot parse my KML and therefore lose functionality that I am trying to test. I've posted a single generic KML file on a publicly available site, but then have to have different development code to render one thing vs. something else when running for real.

本地开发有哪些选项可以呈现公开可用的动态生成的 KML 文件?

What are my options for local development to render what would be publicly available dynamically generated KML files?

推荐答案

您似乎已经很好地概述了这些选项:

It seems like you've outlined the options pretty well:

如果您想处理本地数据,而不涉及可公开访问的网络服务器,您需要使用基于 JavaScript 的方法来解析 KML 并将其加载到地图上.虽然这不会完全复制 Google 的功能,但如果您只关心显示 KML 功能,它对于初始开发来说可能已经足够了.在这种情况下,我可能会设置一个存根类,如下所示:

If you want to work with local data, without involving a publicly accessible webserver, you'll need to use a javascript-based approach to parse the KML and load it onto the map. While this won't perfectly replicate the Google functionality, it is likely good enough for initial development if you only care about displaying the KML features. In this case, I'd probably set up a stub class, like this:

    // I'll assume you have a global namespace called MyProject
    MyProject.LOCAL_KML = true;

    MyProject.KmlLayer = function(url) {
        // parse the KML, maybe caching an array of markers or polygons,
        // using one of the libraries you list in your question
    };

    // now stub out the methods you care about, based on
    // http://code.google.com/apis/maps/documentation/javascript/reference.html#KmlLayer
    MyProject.KmlLayer.prototype.setMap = function(map) {
        // add the markers and polygons to the map, or remove them if !map
    }
    // etc

现在可以在代码中添加一个开关,或者注释/取消注释,或者使用构建脚本进行切换,或者无论您当前的流程是在开发代码还是生产代码之间切换:

Now either put a switch in the code, or comment/uncomment, or use a build script to switch, or whatever your current process is for switching between dev and production code:

    var kmlPath = "/my.kml";
    var kmlLayer =  MyProject.LOCAL_KML ?
        new MyProject.KmlLayer(MyProject.LOCAL_KML_HOST + kmlPath) :
        new google.maps.KmlLayer(MyProject.PRODUCTION_KML_HOST + kmlPath);
    kmlLayer.setMap(myMap);

另一方面,如果您需要 Google KmlLayer 中的所有功能,或者您想确保生产设置能够正常工作,或者您不想打扰 Google 提供的功能,那么您需要将其上传到公共可用的服务器,以便 Google 可以进行服务器端处理.

If, on the other hand, you need all of the functionality in the Google KmlLayer, or you want to make sure things work with the production setup, or you don't want to bother stubbing out the functionality Google provides, then you'll need to upload it to a publicly available server, so that Google can do its server-side processing.

除了明显的选项(FTP、用于上传新 KML 文件的命令行脚本等),其中大多数选项要求您在加载地图页面之前手动执行某些操作,您可能会考虑将更新构建到您正在加载的页面.根据您使用的平台,这在后端或前端可能更容易实现;关键是在您的公共服务器上有一个允许更新 KML 的脚本:

Aside from the obvious options (FTP, a command-line script to upload your new KML file, etc), most of which require you to do something manually before you load your map page, you might consider building the update into the page you're loading. Depending on the platform you're using, this might be easier to do on the back-end or the front-end; the key would be to have a script on your public server that would allow the KML to be updated:

  1. 从 request.POST 中获取 KML 字符串
  2. 验证 KML 字符串(这样您就不会打开服务器受到攻击)
  3. 写入单个文件,例如我的.kml"

然后,当您查看地图页面时,根据来自 localhost 的数据更新远程 KML.这是一个使用 jQuery 的客户端版本:

Then, when you view your map page, update the remote KML based on the data from localhost. Here's a client-side version, using jQuery:

// again, you'd probably have a way to kill this block in production
if (MyProject.UPDATE_KML_FROM_LOCALHOST) {
    // get localhost KML
    $.get(MyProject.LOCAL_KML_HOST + kmlPath, function(data) {
        // now post it to the remote server
        $.post(
            MyProject.DEV_KML_HOST + '/update_kml.php', 
            { kml: data }, 
            function() {
                // after the post completes, get the KML layer from Google
                var kmlLayer new google.maps.KmlLayer(MyProject.DEV_KML_HOST + kmlPath);
                kmlLayer.setMap(myMap);
            }
        );
    })
}

诚然,这里有很多往返(页面 -> localhost,页面 -> 远程服务器,谷歌 -> 远程服务器,谷歌 -> 页面),所以这会em>.但它可以让 Google 的代码正确呈现在本地主机上生成的动态 KML 数据,而不必每次重新加载页面时都执行单独的手动步骤.

Admittedly, there are a lot of round-trips here (page -> localhost, page -> remote server, Google -> remote server, Google -> page), so this is going to be slow. But it would allow you to have Google's code properly render dynamic KML data produced on localhost, without having to take a separate manual step every time you reload the page.

这篇关于Google Maps API 和 KML 文件 LocalHost 开发选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆