首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google地图Geojson infowindow

Google地图Geojson infowindow
EN

Stack Overflow用户
提问于 2018-01-07 14:39:50
回答 1查看 3.2K关注 0票数 5

我正试图建立一个网站,在过去的200年里,在欧洲展示某些流星撞击。我把标记记下来了,但我很难弄清楚如何让它们弹出一个信息窗口,里面还有附加的其他信息(海量、id、位置等)。有人能帮我吗?

代码语言:javascript
复制
  var map;
  function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {lat: 45, lng: -1}
  });

  map.data.loadGeoJson(
      'Meteorite.geojson');
  }
  function information(results) {
  map.data.addGeoJson(results);
  map.data.addListener('click', function(e) {
    infowindow.setPosition(e.latLng);
    infowindow.setContent(e.feature.getProperty("id"));
    infowindow.open(map);
  });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-07 17:13:30

下面是一个使用map.data.addGeoJson (从局部变量加载geojson )的示例,但与map.data.loadGeoJson完全相同。在单击某个功能时,infoWindow显示了相关的属性:

代码语言:javascript
复制
var map;
var infowindow = new google.maps.InfoWindow();

function initialize() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 8,
    center: {lat: 45.062, lng: 7.67},
    mapTypeId: 'terrain'
  });

  // Load GeoJSON.
  map.data.addGeoJson({
      "type": "FeatureCollection",
      "features": [{
        "type": "Feature",
        "geometry": {
          "title": "Mount Viso",
          "type": "Point",
          "coordinates": [7.090301513671875,44.66743203082226]
        },
        "properties": {
          "name": "Mount Viso",
          "description": "the highest mountain of the Cottian Alps",
          "link": "https://en.wikipedia.org/wiki/Monte_Viso"
        }
      }, {
        "type": "Feature",
        "geometry": {
       	  "title": "Gran Paradiso",
          "type": "Point",
          "coordinates": [7.266082763671875,45.51837616409498]
        },
        "properties": {
          "name": "Gran Paradiso",
          "description": "a mountain in the Graian Alps in Italy",
          "link": "https://en.wikipedia.org/wiki/Gran_Paradiso"
        }
      }]
    });
    
    map.data.addListener('click', function(event) {
     var feat = event.feature;
     var html = "<b>"+feat.getProperty('name')+"</b><br>"+feat.getProperty('description');
     html += "<br><a class='normal_link' target='_blank' href='"+feat.getProperty('link')+"'>link</a>";
     infowindow.setContent(html);
     infowindow.setPosition(event.latLng);
     infowindow.setOptions({pixelOffset: new google.maps.Size(0,-34)});
     infowindow.open(map);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
代码语言:javascript
复制
html, body, #map-canvas {
    height: 100%;
    margin: 0;
    padding: 0;
}
代码语言:javascript
复制
<div id="map-canvas"></div>

<script src="https://maps.googleapis.com/maps/api/js"></script>

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48138226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档