-1

I'm doing an important homework for university and I want to use cartoDB to do some parts of it. On my joomla website I have created a form with Chronoforms and in Chronoforms in a custom code box I have written a php code that took the user's data from the form and retrieve some informations from a database. Now the result is an array of names of countries ordered by some parameters the user gave to me. What I want to do now is to show on my map of countries and provinces only the countries in the array in different colours. the effect should be similar to what I can obtain opening the file html below on a browser web only there are more countries to show,with different colours and the name of the countries are taken from the array.

<html>
<head>
  <link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" />
  <script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
  <!--[if lte IE 8]>
    <link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v2/themes/css/cartodb.ie.css" />
  <![endif]-->
  <style>
    html, body {width:100%; height:100%; padding: 0; margin: 0;}
    #cartodb-map { width: 100%; height:100%; background: white;}
  </style>
  <script>
    var map;
    function init(){
      // initiate leaflet map
      map = new L.Map('cartodb-map', {
        center: [10,10],
        zoom: 3
      })
      var layerUrl = 'http://obiettivolaurea.cartodb.com/api/v2/viz/39521542-53a2-11e4-85f3-0e018d66dc29/viz.json' ;
      var subLayerOptions = {
             sql: "SELECT * FROM ne_10m_admin_1_states_provinces where admin='Argentina'"
      }
      cartodb.createLayer(map, layerUrl)
          .addTo(map)
          .on('done', function(layer) {
              layer.getSubLayer(1).set(subLayerOptions);
          }).on('error', function() {
            //log the error
            });
    }
  </script>
</head>
<body onload="init()">
  <div id='cartodb-map'></div>
</body>
</html>

How can I do so?? Someone can help me??

1

You'll need to do two things:

First, rewrite the SQL query to only show the countries contained in the array using the Array comparison, IN:

SELECT * FROM table WHERE country_code IN ('ESP', 'NOR', 'FRA')

Then, provide the CartoCSS to color the geometry of the countries to the subLayerOptions:

cartocss: '#table_name { [country_code='FRA'] { polygon-fill: #4B80B7 }; [country_code='ESP'] { polygon-fill: #E95748 }}'

Resources:

  • Now the subLayerOptions is var subLayerOptions = { sql : "SELECT * FROM ne_10m_admin_1_states_provinces WHERE admin IN ('Spain', 'Norway', 'France')", cartocss: '#ne_10m_admin_1_states_provinces { [admin='France'] { polygon-fill: #4B80B7 }; [admin='Spain'] { polygon-fill: #E95748 }}' } but there must be something wrong with the syntax because it says SyntaxError: missing } after property list ...states_provinces { [admin='France'] { polygon-fill: #4B80B7 }; [admin='Spain'] {... – narusegawa Oct 29 '14 at 5:30
  • Another question:the names of the countries are in my array,so I should not insert them manually in the sql statement but it should be parametric. How can I do it?? – narusegawa Oct 29 '14 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.