【问题标题】:Creating Infowindows on features loaded via loadGeoJson()在通过 loadGeoJson() 加载的功能上创建 Infowindows
【发布时间】:2014-07-11 22:13:33
【问题描述】:

如果这是基本的,我很抱歉,但我的 JavaScript 知识非常有限。

我正在制作一张地图,用于加载我在 ArcGIS 中创建的 GeoJSON 数据,这些数据使用 ogr2ogr 重新格式化为 GeoJSON。我已经加载了地图并显示了我的 GeoJSON 文件中的点标记,我什至还有一个 styleFeature() 函数来根据它们的属性设置特征的样式。

我遇到的问题是在单击点要素时尝试弹出信息窗口。

我已成功使用代码设置事件侦听器并使用来自单击功能的信息更新 div 的内容:

map.data.loadGeoJson('http://www.myurl.com/file.json');

map.data.setStyle(styleFeature);

map.data.addListener('click', function(event) {
    var myHTML = event.feature.getProperty('Description');
    document.getElementById('info-box').innerHTML = myHTML;
});

我想做的是有一个事件启动这样的信息窗口,但它不起作用:

map.data.loadGeoJson('http://www.myurl.com/file.json');

map.data.setStyle(styleFeature);

map.data.addListener('click', function(event) {
    var myHTML = event.feature.getProperty('Description');
    var infowindow = new google.maps.InfoWindow({content: myHTML});
});

我的数据集包含一千多个点,因此硬编码 infowindows 不起作用,而且我还没有看到任何示例显示如何创建 infowindows 数组,因为功能在调用的函数中循环通过setStyle() 要么。

我知道这与我对范围、事件和对象数组缺乏了解有关,但我只是碰壁了。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript google-maps-api-3 infowindow geojson


    【解决方案1】:

    要让信息窗口在点击时显示,您需要调用 open()。

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

    working fiddle

    代码sn-p:

    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!');
    }
    
    function initialize() {
      // Create a simple map.
      features=[];
      map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 4,
        center: {lat: -28, lng: 137.883}
      });
        google.maps.event.addListener(map,'click',function() {
            infowindow.close();
        });
        map.data.setStyle({fillOpacity:.8});
      // Load a GeoJSON from the same server as our demo.
      var featureId = 0;
      google.maps.event.addListener(map.data,'addfeature',function(e){
          if(e.feature.getGeometry().getType()==='Polygon'){
              features.push(e.feature);
              var bounds=new google.maps.LatLngBounds();
              
              e.feature.getGeometry().getArray().forEach(function(path){
              
                 path.getArray().forEach(function(latLng){bounds.extend(latLng);})
              
              });
              e.feature.setProperty('bounds',bounds);
              e.feature.setProperty('featureNum',features.length-1);
              
              
            
            }
      });
      // When the user clicks, open an infowindow
      map.data.addListener('click', function(event) {
              var myHTML = event.feature.getProperty("Description");
          infowindow.setContent("<div style='width:150px; text-align: center;'>"+myHTML+"</div>");
              infowindow.setPosition(event.feature.getGeometry().get());
          infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
              infowindow.open(map);
      });    
       map.data.addGeoJson(googleJSON);
      
      
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    var googleJSON = {
      "type": "FeatureCollection",
      "features": [
        {
          "id":0,
          "type": "Feature",
          "properties": {
            "letter": "G",
            "color": "blue",
            "rank": "7",
            "ascii": "71",
            "Description": "the letter G"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [123.61, -22.14]
             
          }
        },
        {
          "id":1,
          "type": "Feature",
          "properties": {
            "letter": "o",
            "color": "red",
            "rank": "15",
            "ascii": "111",
            "Description": "the first letter o"          
          },
          "geometry": {
            "type": "Point",
            "coordinates": [128.84, -25.76]
          }
        },
        {
          "id":2,
          "type": "Feature",
          "properties": {
            "letter": "o",
            "color": "yellow",
            "rank": "15",
            "ascii": "111",
            "Description": "the second letter o"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [131.87, -25.76]
          }
        },
        {
          "id":3,
          "type": "Feature",
          "properties": {
            "letter": "g",
            "color": "blue",
            "rank": "7",
            "ascii": "103",
            "Description": "the letter g"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [138.12, -25.04]
          }
        },
        {
          "id":4,
          "type": "Feature",
          "properties": {
            "letter": "l",
            "color": "green",
            "rank": "12",
            "ascii": "108",
            "Description": "the letter l"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [140.14,-21.04]
          }
        },
        {
          "id":5,
          "type": "Feature",
          "properties": {
            "letter": "e",
            "color": "red",
            "rank": "5",
            "ascii": "101",
            "Description": "the letter e"
          },
          "geometry": {
            "type": "Point",
            "coordinates": [144.14, -27.41]
          }
        }
      ]
    };
    html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?ext=.js"></script>
    <div id="map-canvas"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 2022-07-30
      • 1970-01-01
      • 2012-03-26
      • 2021-11-26
      • 2014-05-11
      • 1970-01-01
      相关资源
      最近更新 更多