This map visualization shows how to add a click infowindow object with an image to a CartoDB map visualization using CartoDB.js.
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>#DrawDisability</title> | |
| <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | |
| <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> | |
| <link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" /> | |
| <link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" /> | |
| <script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script> | |
| <style> | |
| html, body, #map { | |
| height: 100%; | |
| padding: 0; | |
| margin: 0; | |
| } | |
| div.cartodb-popup.v2 { | |
| width: 180px; | |
| } | |
| .infowindow-custom{ | |
| position: relative; | |
| width: 180px; | |
| background-color: white; | |
| margin-bottom: 11px; | |
| } | |
| .cartodb-popup-close-button { | |
| position: absolute; | |
| top: -12px; | |
| right: -11px; | |
| width: 26px; | |
| height: 26px; | |
| background: url('http://libs.cartodb.com/cartodb.js/v3/themes/img/light.png') no-repeat 0 -23px; | |
| text-indent: -9999px; | |
| font-size: 0; | |
| line-height: 0; | |
| opacity: 1; | |
| text-transform: uppercase; | |
| z-index: 3; | |
| } | |
| .cartodb-popup-content-wrapper { | |
| width: 180px; | |
| } | |
| .cartodb-popup-content { | |
| width: 180px; | |
| } | |
| .cartodb-popup-tip-container { | |
| position: absolute; | |
| bottom: -3px; | |
| left: 23px; | |
| width: 16px; | |
| height: 14px; | |
| background: url('http://libs.cartodb.com/cartodb.js/v3/themes/img/light.png') no-repeat -23px -7px; | |
| text-indent: -9999px; | |
| font-size: 0; | |
| line-height: 0; | |
| opacity: 1; | |
| z-index: 3; | |
| } | |
| blockquote { | |
| display: hidden; | |
| } | |
| .cartodb-popup-content { | |
| /* add styles here */ | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="map"></div> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | |
| <script type="infowindow/html" id="infowindow_template"> | |
| <div class="cartodb-popup v2"> | |
| <div class="infowindow-custom"> | |
| <a href="#close" class="cartodb-popup-close-button close">x</a> | |
| <div class="cartodb-popup-content-wrapper"> | |
| <div class="cartodb-popup-content"> | |
| <img width="100%" src="{{content.data._url}}"></src> | |
| <!-- content.data contains the field info --> | |
| <h4>{{content.data.name}}, {{content.data.age}}, {{content.data.city}}, {{content.data.country}}</h4> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="cartodb-popup-tip-container"></div> | |
| </div> | |
| </script> | |
| <script> | |
| function main() { | |
| var map = L.map('map', { | |
| zoomControl: true, | |
| center: [0, 0], | |
| zoom: 2, | |
| search: true | |
| }); | |
| L.tileLayer('http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.png', { | |
| attribution: 'Stamen' | |
| }).addTo(map); | |
| cartodb.createLayer(map, 'https://team.cartodb.com/u/chandra/api/v2/viz/0249d564-6d3a-11e5-a49e-0e5db1731f59/viz.json', {tooltip: false}) | |
| .addTo(map) | |
| .on('done', function(layer) { | |
| var visitLayer = layer.getSubLayer(0); | |
| visitLayer.setInteraction(true); | |
| cdb.vis.Vis.addInfowindow(map, layer.getSubLayer(0), ['name', 'age', 'city', 'country', '_url'], { | |
| infowindowTemplate: $('#infowindow_template').html(), | |
| templateType: 'mustache' | |
| }); | |
| }).on('error', function() { | |
| console.log("some error occurred"); | |
| }); | |
| } | |
| window.onload = main; | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment