4

I'm using Google Maps to highlight a bunch of countries using Fusion Tables to grab the geometry. You can see an example of this here:

http://jsfiddle.net/4mtyu/689/

var layer = new google.maps.FusionTablesLayer({
  query: {
    select: locationColumn,
    from: tableId,
    where: "ISO_2DIGIT IN ('AF','AL','DZ','AD','AO','AG','AR','AM','AU','AT','AZ','BS','BH','BD','BB','BY','BE','BZ','BJ','BT','BO','BA','BW','BR','BN','BG','BF','BI','KH','CM','CA','CV','CF','TD','CL','CN','CO','KM','CG','CD','CR','HR','CU','CY','CZ','DK','DJ','DM','DO','EC','EG','SV','GQ','ER','EE','ET','FJ','FI','FR','GA','GM','GE','DE','GH','GR','GD','GT','GN','GW','GY','HT','HN','HU','IS','IN','ID','CI','IR','IQ','IE','IL')"
  },
  options : {suppressInfoWindows:true},
  styles: [{
    polygonOptions: {
      fillColor: "#000000",
      strokeWeight: "0",
      fillOpacity: 0.4
    }
  }]
});

The problems begin when I try to grab too many items from the table. Google uses a URL with all of the query values to grab the data required and with URL encoding it can grow to be quite large in length.

You can see an example of the URL here if you open up the console and check the URLs being thrown in the errors:

http://jsfiddle.net/4mtyu/690/

The URL it generates in that particular example is 3749 characters, way over the 2048 character limit.

Does anybody have any ideas on a way I could prevent the URL from getting this large but at the same time still be able to select 150+ items?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
MarioD
  • 1,703
  • 1
  • 14
  • 24
  • 2
    related question: [Google Maps - FusionTablesLayer - max number of items](http://stackoverflow.com/questions/32682691/google-maps-fusiontableslayer-max-number-of-items). From the comments there: Can you add a column to your table that would allow you to select those rows with a simpler query? You might want to add the [google-fusion-tables](http://stackoverflow.com/questions/tagged/google-fusion-tables) tag, I have seen Google Developer advocates active there more often. – geocodezip Oct 07 '15 at 03:16
  • Do you need a normal map, or would a Geomap work? – Burdock Oct 12 '15 at 22:43
  • I need a normal map for this situation. – MarioD Oct 12 '15 at 22:47

1 Answers1

6

The easiest solution is to move things client-side: http://jsfiddle.net/4mtyu/725/


Part 1 :: Initialize the map and fusion tables

You can do this how ever you prefer, just make sure the fusion tables have all countries selected. Example:

function initialize() {
    //settings
    var myOptions = {
      zoom: 2,
      center: new google.maps.LatLng(10, 0),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    //get map div
    map = new google.maps.Map(document.getElementById('map_div'),
        myOptions);

    // Initialize padded JSON request
    var script = document.createElement('script');
    var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
    url.push('sql=');

    //select all the countries!! 
    var query = 'SELECT name, kml_4326 FROM ' +
        '1foc3xO9DyfSIF6ofvN0kp2bxSfSeKog5FbdWdQ';
    var encodedQuery = encodeURIComponent(query);

    //generate URL 
    url.push(encodedQuery);
    url.push('&callback=drawMap');//Callback
    url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ');//select all countries
    script.src = url.join('');

    //Add Script to document
    var body = document.getElementsByTagName('body')[0];
    body.appendChild(script);
  }

Part 2 :: Sort countries and render

  • (a) Once you have the full list of countries, you have to sort them. A simple indexOf check should do the trick.

  • (b) After sorting we need turn our countries into LatLon Coordinates, this is done in the constructNewCoordinates function (see below)

  • (c) Then all that's left is to generate the polygon and add it to our map!

Example:

var countries = [...];

//This is the callback from the above function
function drawMap(data) {
    //Get the countries 
    var rows = data['rows'];


    for (var i in rows) {
      // (a) //
      //If the country matches our filled countries array
      if (countries.indexOf(rows[i][0]) !== -1)

        var newCoordinates = [];

        // (b) //
        // Generate geometries and
        // Check for multi geometry countries 
        var geometries = rows[i][1]['geometries'];
        if (geometries) {
          for (var j in geometries) {
            //Calls our render function, returns Polygon Coordinates (see last step);
            newCoordinates.push(constructNewCoordinates(geometries[j]));
          }
        } else {
          //Calls our render function, returns Polygon Coordinates (see last step);
          newCoordinates = constructNewCoordinates(rows[i][1]['geometry']);
        }

        // (c) //
        //Generate Polygon
        var country = new google.maps.Polygon({
          paths: newCoordinates,
          strokeWeight: 0,
          fillColor: '#000000',
          fillOpacity: 0.3
        });


       //add polygon to map
        country.setMap(map);
      }
    }
  }
}

Part 3 :: Generating the coordinates

// (b) //
function constructNewCoordinates(polygon) {
    var newCoordinates = [];
    var coordinates = polygon['coordinates'][0];
    for (var i in coordinates) {
      newCoordinates.push(
          new google.maps.LatLng(coordinates[i][1], coordinates[i][0]));
    }
    return newCoordinates;
  }
approxiblue
  • 6,982
  • 16
  • 51
  • 59
Burdock
  • 1,085
  • 1
  • 9
  • 22
  • Thanks you for taking the time to provide an amazing answer, it seems to work fine on initial load. I'm having a hard time loading the functions again once the 'countries' have updated after the initial load. What would be the best way to run through these functions again without removing the script and going through all of the steps again? Or is that the only way? – MarioD Oct 13 '15 at 01:25
  • 1
    Just to clarify. You would like the map to updatable? If so I will edit my answer to make the functions more modular! – Burdock Oct 13 '15 at 02:52
  • Hey, yup - so initially the map starts with 0 countries, over time the user inserts countries - so I have to update the polygon with new co-ordinates. – MarioD Oct 13 '15 at 04:29
  • 1
    Here is an updated script :: http://jsfiddle.net/4mtyu/733/. Feel free to shoot me a chat msg if you want more info – Burdock Oct 13 '15 at 05:20
  • Thank you for taking the time to provide a detailed answer, I owe you one! – MarioD Oct 16 '15 at 14:50