davestacey
2/17/2015 - 7:49 PM

Get the map overlay shapes for US and Canada, add only the ones the user selecten in Drupal, color based on parent region

Get the map overlay shapes for US and Canada, add only the ones the user selecten in Drupal, color based on parent region

// Get the features to add to the map
      nsmap.getFeatures = function (thisMap, statesSelected, callback){
        var map = thisMap;
        var theStates = statesSelected;
        var featureFilePath = this.basePath + '/data/us-states.js';
        var provinceFilePath = this.basePath + '/data/canada.json';

        // empty feature collection to add geojson
        var colObj = {
          type: 'FeatureCollection',
          features: []
        };

        // Step 1 get the us-state features
        getUSfCollection(featureFilePath, function(errorMessage, usresults) {
          if (errorMessage){
            console.error('getUSfCollection failed: US state file is a dependency for mapping.');
            callback('getUSfCollection failed');
          };          
          nsmap.allFeaturesToSearch.features = nsmap.allFeaturesToSearch.features.concat(usresults.features); // all the state shapes
          // Step 2 get the Canada features
          getCanadafCollection(provinceFilePath, function(canresults) {
            nsmap.allFeaturesToSearch.features = nsmap.allFeaturesToSearch.features.concat(canresults.features);
            // We have all the features, now apply to the selected states
            for (var i = theStates.length - 1; i >= 0; i--) {
              //console.log('getFeatures - theStates[i]', theStates[i]);
              var aStateNameToFind = theStates[i].name.toUpperCase() ;
              var featureArray = nsmap.allFeaturesToSearch.features;
              if ($.type(featureArray) === "array") {
                // Loop Features and find either a canadian province or a US state
                for (var j = featureArray.length - 1; j >= 0; j--) {
                  var featureNameUS = featureArray[j].properties.name ;
                  var featureNameCanada = featureArray[j].properties.NAME ;
                  var featureNameFound = '';
                  if(featureNameUS ){
                    featureNameFound = featureNameUS.toUpperCase();
                  } else if (featureNameCanada) {
                    featureNameFound = featureNameCanada.toUpperCase();
                  };
                  if (featureNameFound ){
                    if (aStateNameToFind == featureNameFound){
                      //console.log('-getFeatures - matched a location, now get the details');

                      // Collect the parent info (for coloring etc)
                      // Note: now we only get one parent per item, no need to loop.
                      findByNameGetParents(featureNameFound, function(parent){
                        var parentArray = parent; // Assuming just one parent
                        //console.log('returned parent TID: ', parentArray);
                        findParentByTID(parentArray, function(parentObject){
                          if (parentObject) {
                            //console.log('Returned Parent Object: ', parentObject);
                            featureArray[j].properties.parent = parentObject.name;
                            //console.log('Added Parent to featureArray[j]', featureArray[j]);
                          }
                        }); //findParentByTIDFromID
                      }); //findByNameGetParents

                      // push it to the collection
                      colObj.features.push(featureArray[j]);
                    };
                  };
                }; // for feature
              } else {
                console.error('featureArray is not available');
              }
            }// for state
            callback(colObj);
          })//getCanadafCollection
        })//getUSfCollection
      }//getFeatures