0

Is it possible to show a CartoDB infowindow for a particular point on a map programmatically? I want to open the info window when a marker is dropped on a Google Map.

2

You can do that by simulating a click event, check this example from xavijam here: http://bl.ocks.org/xavijam/ae5317bddf1256f91534

Also, copying his code here for the record:

<!DOCTYPE html>
<html>
  <head>
    <style>
      html, bo

dy, #map {
        height: 100%;
        padding: 0;
        margin: 0;
      }
      .list {
        position: absolute;
        background: white;
        top: 180px;
        left: 20px;
        padding: 20px;
        z-index: 10;
      }
</style>

</head>
  <body>
    <div id="map"></div>
    <div class="list">
  <a href="#" class="link">open infowindow</a>

</div>


<script>
  function openInfowindow(layer, latlng, cartodb_id) {
    layer.trigger('featureClick', null, latlng, null, { cartodb_id: cartodb_id }, 0);
  }


  function main() {

    cartodb.createVis('map', 'http://documentation.cartodb.com/api/v2/viz/7eb2096a-51d9-11e3-89a7-5404a6a683d5/viz.json', {
        shareable: false,
        title: false,
        description: false,
        search: false,
        tiles_loader: true,
        center_lat: 42,
        center_lon: -3,
        zoom: 6,
        legends: false
    })
    .done(function(vis, layers) {
      // layer 0 is the base layer, layer 1 is cartodb layer
      var layer = layers[1];
      $('.link').click(function() {
        openInfowindow(layer, [40.4000262645, -3.683351686], 5324)
        return false;
      });

    })
    .error(function(err) {
      console.log(err);
    });
  }

  window.onload = main;
</script>

  • Thanks for this, plus the hint on the uncompressed JS library. – Adam Baxter Feb 19 '15 at 23:00
  • Actually, I'm going to edit my response because the non-versioned v3 URLs do no longer contain the last CartoDB.js version :) – iriberri Jun 24 '15 at 8:55

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.