Google Maps Infowindow显示特定内容

时间:2016-11-12 18:21:07

标签: javascript jquery google-maps geojson infowindow

我正在尝试显示Infowindow机场的信息:

enter image description here

从.GEOjson文件中检索机场的信息:



{ "type": "Feature", "properties": { "Name": "Epps Airpark", "Description": "description: id: 6525
<br>ident: 00AL
<br>type: small_airport
<br>latitude_deg: 34.86479949951172
<br>longitude_deg: -86.77030181884766
<br>elevation_ft: 820
<br>continent: NA
<br>iso_country: US
<br>iso_region: US-AL
<br>municipality: Harvest
<br>scheduled_service: no
<br>gps_code: 00AL
<br>iata_code:
<br>local_code: 00AL
<br>home_link:
<br>wikipedia_link:
<br>keywords:
<br>id: 6525.0
<br>ident: 00AL
<br>type: small_airport
<br>latitude_deg: 34.86479949951172
<br>longitude_deg: -86.77030181884766
<br>elevation_ft: 820.0
<br>continent: NA
<br>iso_country: US
<br>iso_region: US-AL
<br>municipality: Harvest
<br>scheduled_service: no
<br>gps_code: 00AL
<br>iata_code:
<br>local_code: 00AL
<br>home_link:
<br>wikipedia_link:
<br>keywords: " }, "geometry": { "type": "Point", "coordinates": [ -86.770302, 34.864799, 0.0 ] } }
&#13;
&#13;
&#13;

我想从Infowindow中删除一些项目,例如wikipedia_link,local_code和id。

Infowindow代码如下:

&#13;
&#13;
var infowindow = new google.maps.InfoWindow();

function gotoFeature(featureNum) {
  var feature = map.data.getFeatureById(features[featureNum].getId());
  if (!!feature) google.maps.event.trigger(feature, 'changeto', {
    feature: feature
  });
  else alert('feature not found!');
}

map.data.addListener('click', function(event) {
  var myHTML = event.feature.getProperty("Description");
  infowindow.setContent("<div style='width:250px; text-align: center; '>" + myHTML + "</div>");
  infowindow.setPosition(event.feature.getGeometry().get());
  infowindow.setOptions({
    pixelOffset: new google.maps.Size(0, -30)
  });
  infowindow.open(map);
});
&#13;
&#13;
&#13;

你如何防止在Infowindow上显示某些项目(例如wikipedia_link,local_code和id)?

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是将HTML描述拆分为数组并过滤掉您不想显示的项目

var myHTML = event.feature.getProperty("Description");

var categories = ["wikipedia_link", "local_code", "id"]; // the categories to exclude

function excludeCat(d) {
  if (categories.indexOf(d.split(":")[0]) < 0) {
    return d
  };
};

myHTML = myHTML.split("<br>").filter(excludeCat).join("<br>");