简体   繁体   中英

How can I access dataobjects in geoJSON data, placed on Google Maps

What I've done: I'm working with the Google Maps Javascript API. My geodata is stored in a geoJSON file. I've placed the data on the map using a data layer. I've made a clickEvent for showing popUpWindows.

What I want: I want to show a circle only on markers that have the value 'school' in the 'category' property.

My geoJSON looks like this:

{
  "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]
            }
        }
    ]
}

My javascript look like this:

<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>

The Question : How can accomplish this?

One option is to use a listener on the "addfeature" event of the DataLayer. Note that javascript is case sensitive so "school" is not the same as "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
    });
  }
});

proof of concept fiddle

code snippet:

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM