我的谷歌放置kml网址javascript

时间:2016-09-26 22:55:23

标签: javascript jquery google-maps google-maps-api-3 kml

我的谷歌地图中的地方有问题,我已经在我的https服务器上有一个文件kml的功能图,但我不想每次做出更改时下载和上传地图,而不是为我工作只嵌入我需要用API操纵,所以这是我的代码:

var map;
var src = 'MY_SERVER/points_vl.kmz';
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: new google.maps.LatLng(20.63736, -105.22883),
    zoom: 2,
});

loadKmlLayer(src, map);
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };
        var circle = 'http://www.nearby.org.uk/google/circle.kml.php?radius=5miles&lat='+position.coords.latitude+'&long='+position.coords.longitude;
        loadKmlLayer(circle, map);
        map.setCenter(pos);
        setTimeout(function(){
            var infowindow = new google.maps.InfoWindow({
                map: map,
                position: pos,
                content: 'Current Location'
            });
            infowindow.setPosition(pos);
        }, 2000);
    });
}
}

function loadKmlLayer(src, map) {
var kmlLayer = new google.maps.KmlLayer(src, {
    suppressInfoWindows: true,
    preserveViewport: false,
    map: map
});
google.maps.event.addListener(kmlLayer, 'click', function(event) {
    var content = event.featureData.infoWindowHtml;
    var testimonial = document.getElementById('capture');
    testimonial.innerHTML = content;
});

}

这项工作很好,但是有办法直接从我的谷歌地图地址中取出kml吗?

Using an existant Google 'My Places' map with Maps API v3 styling这个帖子有一些想法,但是没有用,如果你知道如何让它变得美妙

1 个答案:

答案 0 :(得分:2)

转到您的" MyMap"地图。点击地图名称旁边的三个点,点击"导出到KML":

enter image description here

选择"使用网络链接KML保持数据最新(仅在线使用):

enter image description here

将.kmz文件重命名为.zip,然后打开它并打开它包含的doc.kml文件。该文件将直接链接到指定您的" MyMap"。

的KML数据

google.maps.KmlLayer

中使用该链接

代码段



function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 11,
    center: {
      lat: 41.876,
      lng: -87.624
    }
  });

  var ctaLayer = new google.maps.KmlLayer({
    url: 'http://www.google.com/maps/d/kml?mid=1-mpfnFjp1e5JJ1YkSBjE6ZX_d9w',
    map: map
  });
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#map {
  height: 100%;
}

<div id="map"></div>
<!-- add your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
&#13;
&#13;
&#13;