1

So, the scenario is I have a situation where users can look up multiple tables worth of data, and want to view them in an aggregate fashion. Based on certain filters, I get a list of tables, then want them displayed on a map.

For the sake of everyone's sanity, I've put up a Fiddle with a working version:

https://jsfiddle.net/tigerhawkvok/q1nq89s1/5/

It uses real table names (with test data) and a real account, and you can see it not functional.

Here's the main function in question:

createRawCartoMap = (layers, callback, options, mapSelector = "#global-map-container") ->
  ###
  # Create a raw CartoDB map
  #
  # See
  # https://docs.cartodb.com/cartodb-platform/cartodb-js/getting-started/#creating-visualizations-at-runtime
  #
  # @param array layers -> an array of sublayer objs for CartoDB
  ###
  unless options?
    options = new Object()

  params =
    user_name: options.user_name ? cartoAccount
    type: options.type ? "cartodb"
    sublayers: layers

  # The CartoDB layer options
  mapOptions =
    cartodb_logo: false
    https: true
    mobile_layout: true
    gmaps_base_type: "hybrid"
    center_lat: window.locationData?.lat ? center[0]
    center_lon: window.locationData?.lng ? center[1]
    zoom: 5

  ## Leaflet Map Setup
  leafletOptions =
    center: [window.locationData?.lat ? 0, window.locationData?.lng ? 0]
    zoom: zoom ? 5
  unless geo.lMap?
    lMap = new L.Map(mapSelector.slice(1), leafletOptions)
    geo.lMap = lMap
    lTopoOptions =
      attribution: 'Tiles © Esri — Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community'
    L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}', lTopoOptions).addTo lMap
  BASE_MAP = geo.lMap

  cartodb
  .createLayer(BASE_MAP, params)
  .addTo(BASE_MAP, 1)
  .on "done", (layer) ->
    # It really should already be up from the array of sublayers provided, but sure, we'll try again
    for dataLayer in layers
      layer.createSubLayer dataLayer
    if typeof callback is "function"
      callback()
    false
  .on "error", (errorString) ->
    # Error handling
  false

Both the fiddle and the code above may have some detritus from the full script it was pulled out of.

I'm seriously at a loss. Everything I've read and seen seems to indicate that it should work, but it doesn't.

1 Answer 1

4

Two things here checking on your fiddle. First is you are putting the https:true option in the wrong place, you need to add it to the next parameter. In normal JS it would be like this:

      cartodb.createLayer(map, {
        user_name: 'user',
        filter: "mapnik",
        type: 'cartodb',
        sublayers: [{
          sql: layer_sql,
          cartocss: layer_cartocss,
          interactivity: 'cartodb_id, name, field1,field2'
        }]
      }, {
        https:true // here!
      })

The second error is that your tables are not public. You'll see a response of the Maps API like this on your console

Postgis Plugin: ERROR: permission denied for relation t7189c8441479760839c8fe7ebc3fbe5d_6d6d454828c05e8ceea03c99cc5f5...

In order to create layers with the Maps API on the fly your datasets need to have their privacy set to Public or Link, otherwise you need to use Named Maps and some kind of middleware to generate templates on the fly.

6
  • Hm, I see that I can add the API key to make it work outright (with the implied security risk), and I can set privacy to 'link' when importing tables, but I don't see what the query structure would be to change the privacy via the API ... Otherwise I need to do an authenticated data fetch then feed that manually into createLayer, yeah? Commented Jun 7, 2016 at 0:54
  • Or, possibly, based on this comment something like: POST https://USER.cartodb.com/api/v1/tables/TABLE_NAME?api_key=API_KEY But the thread isn't super clear on that. Or I'm tired and can't read. Commented Jun 7, 2016 at 1:07
  • There's not public API to change privacy of tables and never put your API key on a web application. You should create a middleware application coded in any language you fancy to deal with named maps or proxy your queries to the Maps API adding the API key
    – Jorge Sanz
    Commented Jun 7, 2016 at 14:16
  • Yeah, for testing I just was entering it in the console. (They really need a domain-restricted API key or something ...). Name maps it is! Thanks. Commented Jun 9, 2016 at 19:08
  • Well, I did migrate to Named Maps, and the console command returns something sane ( imgur.com/LzvF0os ), but the live (and fiddle!) still don't show any points: jsfiddle.net/tigerhawkvok/q1nq89s1 ... thoughts? Commented Jun 15, 2016 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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