如何从信息窗口中删除信息(在KmlLayer上)

时间:2015-02-19 21:30:05

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

我正在使用Google Map API,我已将kml图层导入到我的代码中。我的问题是我不知道如何从信息窗口中删除“未知点要素”信息。有什么建议?这是我所说的截图:

enter image description here

这是我导入kml文件的代码:

var AI_url = 'https://drive.google.com/uc?export=download&id=0B2KR4Lz3foYEeEtfR0laWWM0LVk'            
var AI_options = {
    preserveViewport: true,
    map: map
};
var AI_layer = new google.maps.KmlLayer(AI_url, AI_options);

3 个答案:

答案 0 :(得分:2)

信息窗口内容来自KML文件,因此您必须从那里删除它,前提是您可以从当前服务的位置访问该文件。

答案 1 :(得分:1)

来自API: https://developers.google.com/maps/tutorials/kml/

var kmlOptions = {
  **suppressInfoWindows: true,**
  preserveViewport: false,
  map: map
};

答案 2 :(得分:0)

一个选项是设置KmlLayer的suppressInfoWindows选项,然后添加自己的单击监听器,只显示“名称”:

var AI_url = 'https://drive.google.com/uc?export=download&id=0B2KR4Lz3foYEeEtfR0laWWM0LVk'
var AI_options = {
    preserveViewport: true,
    suppressInfoWindows: true,
    map: map
};
var AI_layer = new google.maps.KmlLayer(AI_url, AI_options);
google.maps.event.addListener(AI_layer,'click',function(e) {
    infoWindow.setOptions({
      content:"<b>"+e.featureData.name+"</b>",
      pixelOffset:e.pixelOffset, 
      position:e.latLng
    });
    infoWindow.open(map);
});

概念证明代码段:

var geocoder;
var map;
var infoWindow = new google.maps.InfoWindow();

function initialize() {
  geocoder = new google.maps.Geocoder();
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

  var AI_url = 'https://drive.google.com/uc?export=download&id=0B2KR4Lz3foYEeEtfR0laWWM0LVk'
  var AI_options = {
    preserveViewport: true,
    suppressInfoWindows: true,
    map: map
  };
  var AI_layer = new google.maps.KmlLayer(AI_url, AI_options);
  google.maps.event.addListener(AI_layer, 'click', function(e) {
    infoWindow.setOptions({
      content: "<b>" + e.featureData.name + "</b>",
      pixelOffset: e.pixelOffset,
      position: e.latLng
    });
    infoWindow.open(map);
  });

  codeAddress("Calgary, Canada");

}
google.maps.event.addDomListener(window, "load", initialize);

function codeAddress(address) {
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.fitBounds(results[0].geometry.viewport);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>