如何访问放在谷歌地图上的geoJSON数据中的数据对象?

问题描述 投票:1回答:1

我做了什么。我正在使用谷歌地图的Javascript API。我的地理数据存储在一个geoJSON文件中。我已经使用数据层将数据放置在地图上。我已经做了一个clickEvent来显示popUpWindows。

我想要什么?我想只在 "类别 "属性中含有 "学校 "的标记上显示一个圆圈。

我的 geoJSON 是这样的。

{
  "type": "FeatureCollection",
  "features": [
        {
            "type": "Feature",
            "properties": {
                "category": "School",
                "name":"De Hooge Waai",
                "popupContent": "Basisschool De Hooge Waai in Raamsdonk",
                "icon": "http://maps.google.com/mapfiles/kml/paddle/S.png"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [4.905370,51.686385]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "category": "Museum",
                "name":"Landgoed Het Broeck",
                "popupContent": "Landgoed 'Het Broeck heeft rijtuigmuseum",
                "icon": "http://maps.google.com/mapfiles/kml/paddle/M.png"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [4.900267,51.686103]
            }
        }
    ]
}

我的javascript是这样的

<script>
    function initMap() {

        //----------
        // Map
        //----------
        var mapOptions = {
            zoom: 15,
            center:{lat: 51.687762, lng: 4.909900}
        };

        var map = new google.maps.Map(document.getElementById("map"),mapOptions);

        //-----------
        // Assets:
        //-----------
        infowindow = new google.maps.InfoWindow({
            content: ""
        });
        var regionCircle = new google.maps.Circle({
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35,
            radius: 500
        });


        // JSON:
        map.data.loadGeoJson('test.json');


        // ICON:
        map.data.setStyle(function(feature) {
            return ({
                icon:{
                    url:feature.getProperty('icon'),
                    scaledSize: new google.maps.Size(32, 32)
                }
            });
        });

        //---------------
        // Events:
        //---------------
        map.data.addListener('click', function(event) {
            var myHTML = "<h1>"+event.feature.getProperty("category")+
                         "</h1><h2>"+event.feature.getProperty("name")+"</h2>" +
                         event.feature.getProperty("popupContent");
            infowindow.setContent(myHTML);
            infowindow.setPosition(event.feature.getGeometry().get());
            infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
            infowindow.open(map);


        });  
        google.maps.event.addListener(map,'click',function() {
           infowindow.close();
        });
    }
    </script>

问题:如何才能做到这一点?

javascript json google-maps google-maps-api-3 geojson
1个回答
2
投票

一个选择是在DataLayer的 "addfeature "事件上使用一个监听器.注意javascript是区分大小写的,所以 "学校 "和 "School "是不一样的。

map.data.addListener('addfeature', function(evt) {
  if (evt.feature.getProperty('category') == "School") {
    var regionCircle = new google.maps.Circle({
      center: evt.feature.getGeometry().get(),
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      radius: 500,
      map: map
    });
  }
});

概念验证

代码片段。

function initMap() {

  //----------
  // Map
  //----------
  var mapOptions = {
    zoom: 14,
    center: {
      lat: 51.687762,
      lng: 4.909900
    }
  };

  var map = new google.maps.Map(document.getElementById("map"), mapOptions);

  //-----------
  // Assets:
  //-----------
  infowindow = new google.maps.InfoWindow({
    content: ""
  });
  var regionCircle = new google.maps.Circle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    radius: 500
  });


  map.data.addListener('addfeature', function(evt) {
    if (evt.feature.getProperty('category') == "School") {
      var regionCircle = new google.maps.Circle({
        center: evt.feature.getGeometry().get(),
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        radius: 500,
        map: map
      });
    }
  });

  // JSON:
  map.data.addGeoJson(geoJson);
  // ICON:
  map.data.setStyle(function(feature) {
    return ({
      icon: {
        url: feature.getProperty('icon'),
        scaledSize: new google.maps.Size(32, 32)
      }
    });
  });

  //---------------
  // Events:
  //---------------
  map.data.addListener('click', function(event) {
    var myHTML = "<h1>" + event.feature.getProperty("category") +
      "</h1><h2>" + event.feature.getProperty("name") + "</h2>" +
      event.feature.getProperty("popupContent");
    infowindow.setContent(myHTML);
    infowindow.setPosition(event.feature.getGeometry().get());
    infowindow.setOptions({
      pixelOffset: new google.maps.Size(0, -30)
    });
    infowindow.open(map);


  });
  google.maps.event.addListener(map, 'click', function() {
    infowindow.close();
  });
}
google.maps.event.addDomListener(window, "load", initMap);
var geoJson = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "category": "School",
      "name": "De Hooge Waai",
      "popupContent": "Basisschool De Hooge Waai in Raamsdonk",
      "icon": "http://maps.google.com/mapfiles/kml/paddle/S.png"
    },
    "geometry": {
      "type": "Point",
      "coordinates": [4.905370, 51.686385]
    }
  }, {
    "type": "Feature",
    "properties": {
      "category": "Museum",
      "name": "Landgoed Het Broeck",
      "popupContent": "Landgoed 'Het Broeck heeft rijtuigmuseum",
      "icon": "http://maps.google.com/mapfiles/kml/paddle/M.png"
    },
    "geometry": {
      "type": "Point",
      "coordinates": [4.900267, 51.686103]
    }
  }]
};
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>
© www.soinside.com 2019 - 2024. All rights reserved.