简体   繁体   English

我可以通过JS API从Google地图(再次)加载KML数据吗?

[英]Any way I can load KML data from Google maps (again) via JS API?

I've been working on a little ember app for a while which loaded articles from a local newspapers API and presented it in a nice layout. 我一直在研究一个小的余烬应用程序,该应用程序从本地报纸API加载文章并以一种不错的布局进行展示。 As these articles are all about travel destinations, the navigation is based upon a big map with markers in the background of the page. 由于这些文章都是关于旅行目的地的,因此导航基于在页面背景中带有标记的大地图。 Originally I started out with a leaflet map. 最初我是从传单地图开始的。 Adding a marker for each article was no problem, as I knew their coordinates as they were saved with each article and accessable through the API. 为每篇文章添加标记是没有问题的,因为我知道它们的坐标,因为它们随每篇文章一起保存并可通过API访问。

It got more complicated when I realized that some authors embedded small Goolge maps into their copy with markers for nice sites, bars, etc. As I didn't want to have a Google map displayed over a leaflet map, I was searching for way to get the marker data from the Google maps and use it on my Leaflet one. 当我意识到有些作者将精美的站点,栏等标记嵌入小Goolge地图时,情况变得更加复杂。由于我不想在传单地图上显示Google地图,因此我在寻找方法从Google地图获取标记数据,并在我的Leaflet中使用它。 The solution I ended up working with was pretty crazy but did what I was hoping for: I realized that an HTTP request to a Google map's embed url with the request parameter “output” set to “kml” got me an KML/XML file with all the data of the KML layer used with the map. 我最终使用的解决方案非常疯狂,但是却实现了我所希望的:我意识到对Google地图嵌入URL的HTTP请求的请求参数“ output”设置为“ kml”,这使我得到了一个KML / XML文件地图使用的KML层的所有数据。 To circumvent issues with cross-domain requests I requested the KML data indirectly through Yahoo's YQL service which kindly converted the data to JSON as well. 为了规避跨域请求的问题,我通过Yahoo的YQL服务间接请求了KML数据,该服务也将数据也转换为JSON。

Everything was fine. 一切都很好。 Until it wasn't. 直到不是那样。 Since implementing the recent changes to the API (especially regarding KML stuff), my requests return KMZ (zipped KML) data. 由于实施了对API的最新更改(尤其是有关KML的东西),我的请求返回了KMZ(压缩的KML)数据。 Of course neither YQL conversion nor my Ember app can deal with the now binary data. 当然,YQL转换和我的Ember应用程序都无法处理现在的二进制数据。

It doesn't seem there is any way to get my hands on the uncompressed data again. 似乎没有任何办法可以让我再次接触未压缩的数据。 Is there at least a way to make my main map–which is now a Google map, too–load the kml layer from another map identified by an embed url? 是否至少有一种方法可以使我的主地图(现在也是Google地图)从嵌入网址所标识的另一张地图中加载kml图层? Any hint is welcome and really appreciated. 任何提示都是值得欢迎的,并且真的很感激。 Thanks! 谢谢!

Found the solution myself. 自己找到解决方案。 Since Google is serving KML data only zipped (KMZ), there doesn't seem to be any way anymore to get the actual raw layer data to dynamically build a map from scratch while being able to access and manipulate each marker etc. 由于Google仅提供压缩的KML数据(KMZ),因此似乎不再有任何方法可以获取实际的原始图层数据以从头开始动态构建地图,同时能够访问和操作每个标记等。

At least it is possible to load KML layers by changing the maps' embed urls to make the request deliver KMZ data. 至少可以通过更改地图的嵌入网址来加载KML图层,以使请求传递KMZ数据。

For older embed urls with msid parameter use: 对于具有msid参数的旧版嵌入网址, 使用:

https://maps.google.com/maps/ms?ie=UTF8&msa=0&output=kml&msid= {MSID} https://maps.google.com/maps/ms?ie=UTF8&msa=0&output=kml&msid= {MSID}

For recent embed urls with mid parameter use: 对于带有中间参数的最新嵌入网址,请使用:

https://www.google.com/maps/d/u/0/kml?mid= {MID} https://www.google.com/maps/d/u/0/kml?mid= {MID}

These urls then can be used to create a kmlLayer object using Google Map's JavaScript API like this: 这些网址随后可用于使用Google Map的JavaScript API创建kmlLayer对象,如下所示:

// set up map
var map = new google.maps.Map(document.getElementById('map-canvas'),{ … });

// create kmlLayer object from URL
var kmlUrl = 'https://www.google.com/maps/d/u/0/kml?mid=zWcxp2-PLDWQ.k8l5tfHg76WQ';
var kmlLayer = new google.maps.KmlLayer({ url: kmlUrl });

// add layer to map
kmlLayer.setMap(map);

This will set the view and load all markers and overlays saved with your custom map identified by the url. 这将设置视图并加载由url标识的自定义地图保存的所有标记和叠加层。 So you can use every custom map you created with Google Maps as as KML/KMZ data source to define map views you can display–and switch between–on a single canvas using the JS API. 因此,您可以将通过Google Maps创建的每张自定义地图用作KML / KMZ数据源,以定义可使用JS API在单个画布上显示和切换的地图视图。 Worked for my case. 为我的情况工作。

You can use a KmlLayer 您可以使用KmlLayer

see this google developer sample 看到这个Google开发人员样本

https://developers.google.com/maps/documentation/javascript/examples/layer-kml https://developers.google.com/maps/documentation/javascript/examples/layer-kml

Playing around with the MSID/MIDs and the google My maps I noticed that when you are asked to export a layer it gives you the option to receive a KML. 在使用MSID / MID和Google我的地图时,我注意到当要求您导出图层时,它为您提供了接收KML的选项。 With the MID link, or msid, which gives you a kmz, all you have to do is add &forcekml=1 to receive a kml. 使用MID链接(即msid)为您提供kmz,您只需添加&forcekml = 1即可获得kml。

Like so: 像这样:

https://www.google.com/maps/d/u/0/kml?mid= {MID} &forcekml=1 https://www.google.com/maps/d/u/0/kml?mid= {MID} &forcekml = 1

Doesn't appear to want to load with omnivore however. 但是,似乎并不想加载杂食动物。

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

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