Google Maps API v3: data-level identifiers undefined

the long-awaited lurker, the first poster here, so be gentle ...

I am building a map that uses data processed using MYSQL db via PHP to set the color of the polygons defined by the geoJson file (it uses this on google dev as a template). The problem I am facing is that the data layer will not automatically initialize when the page loads.

Full javascript / HTML is located below, but the code section that was used in the following example to initialize the data layer:

  google.maps.event.addListenerOnce(map.data, 'addfeature', function() {
  google.maps.event.trigger(document.getElementById('price_select'),
        'change');
  });

This gives me the error "Uncaught TypeError: Unable to read the property" setProperty "from undefined". If I comment on the listener, the data layer will load normally, but only after I manually select a new entry from the drop-down list (id = 'price_select').

The geoJson file that I am loading is relatively large (~ 14 Mb), so I think that it happens that the listener starts before the whole file is downloaded ("addfeature" waits only for the first function to be added, but I have> 2000) , and therefore the areas processed by PHP do not yet have the corresponding function identifier, which is set by the parameter idPropertyName: 'Name'in the loadGeoJson call. I do not know how to configure the listener to run only once when the entire GeoJson file is loaded. Alternatively, I can be completely wrong, as this is the cause of the error.

, - , (, loadData , ), , . , !

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script>

        var map;

        var priceMin = 1000000;
        var priceMax = 0;

        google.maps.event.addDomListener(window, 'load', function(){

            map = new google.maps.Map(document.getElementById('map-canvas'), {
            center: new google.maps.LatLng(53.587425,-1.663539),
             zoom: 7,
            }); 

        // add styles
        map.data.setStyle(styleFeature);
        map.data.addListener('mouseover', mouseInToRegion);
        map.data.addListener('mouseout', mouseOutOfRegion);


      // initiate drop down functionality
      var selectBox = document.getElementById('price_select');
      google.maps.event.addDomListener(selectBox, 'change', function() {
        clearData();
        loadData(selectBox.options[selectBox.selectedIndex].value);
      });


      // load polygons
      loadMapShapes();

    });     

    function loadMapShapes(){

     map.data.loadGeoJson('http://localhost/OS_raw.json',
        { idPropertyName: 'Name' }); 

  //This is the listener that is supposed to initiate the data layer

      google.maps.event.addListenerOnce(map.data, 'addfeature', function() {
      google.maps.event.trigger(document.getElementById('price_select'),
            'change');
      });

 // End listener   


    }

    function loadData(variable){

        var phpdistricts = (<?php echo $phpdistricts; ?>);
        var phpprices = (<?php echo $phpprices; ?>);



        for(var i=0; i<phpdistricts.length; i++){

            var district = phpdistricts[i];
            var price = parseInt(phpprices[i]);

            // keep track of min and max values
            if (price < priceMin) {
            priceMin = price;
            }
            if (price > priceMax) {
            priceMax = price;
            }


            console.log(map.data.getFeatureById(district));

   //This is where the error triggers - feature is undefined according to console log above
            map.data
            .getFeatureById(district)
            .setProperty('price', price);}

   //end of problematic section        

            // update and display the legend
            document.getElementById('census-min').textContent =
                priceMin.toLocaleString();
            document.getElementById('census-max').textContent =
                priceMax.toLocaleString();    

    }

    function clearData() {
      priceMin = 1000000;
      priceMax = 0;
      map.data.forEach(function(row) {
        row.setProperty('price', undefined);
      });
      document.getElementById('data-box').style.display = 'none';
      document.getElementById('data-caret').style.display = 'none';
    }

    function styleFeature(feature) {
      var low =  [151, 83, 34]; // color of smallest datum
      var high = [5, 69, 54];   // color of largest datum

      // delta represents where the value sits between the min and max
      var delta = (feature.getProperty('price') - priceMin) /
          (priceMax - priceMin);

      var color = [];
      for (var i = 0; i < 3; i++) {
        // calculate an integer color based on the delta
        color[i] = (high[i] - low[i]) * delta + low[i];
      }

      // filters out areas without data
      var showRow = true;
      if (feature.getProperty('price') == null ||
          isNaN(feature.getProperty('price'))) {
        showRow = false;
      }

      var outlineWeight = 0.5, zIndex = 1;
      if (feature.getProperty('state') === 'hover') {
        outlineWeight = zIndex = 2;
      }

      return {
        strokeWeight: outlineWeight,
        strokeColor: '#fff',
        zIndex: zIndex,
        fillColor: 'hsl(' + color[0] + ',' + color[1] + '%,' + color[2] + '%)',
        fillOpacity: 0.75,
        visible: showRow
      };
    }

    function mouseInToRegion(e) {
      // set the hover state so the setStyle function can change the border
      e.feature.setProperty('state', 'hover');

      var percent = (e.feature.getProperty('price') - priceMin) /
          (priceMax - priceMin) * 100;

      // update the label
      document.getElementById('data-label').textContent =
          e.feature.getProperty('Name');
      document.getElementById('data-value').textContent =
          e.feature.getProperty('price');
      document.getElementById('data-box').style.display = 'block';
      document.getElementById('data-caret').style.display = 'block';
      document.getElementById('data-caret').style.paddingLeft = percent + '%';
    }

    function mouseOutOfRegion(e) {
      // reset the hover state, returning the border to normal
      e.feature.setProperty('state', 'normal');
    }


        </script>
      </head>
      <body>
        <div id="controls" class="nicebox">
            <div>
            <select id="price_select">
                <option value="price">Jun '14</option>
                <option value="price">Jun '14</option>
            </select>
            </div>

            <div id="legend">
            <div id="census-min">min</div>
            <div class="color-key">
                <span id="data-caret"></span>              
            </div>
            <div id="census-max">max</div>          
            </div>
            </div>
        <div id="data-box" class="nicebox">
            <label id="data-label" for="data-value">Area: </label>
            <span id="data-value"></span>
        </div>
        <div id="map-canvas"></div>
      </body>
    </html>   
+4
1

, , , , ?

map.data.setStyle(
 function(feature){
   // Build your styles here based on feature properties.

    return style_i_want_for_this_feature;
 }
);

GeoJSON, . , .

0

All Articles