Skip to content

Instantly share code, notes, and snippets.

@juanignaciosl
Forked from javisantana/index.html
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanignaciosl/7b6f75a4edf875f8a778 to your computer and use it in GitHub Desktop.
Save juanignaciosl/7b6f75a4edf875f8a778 to your computer and use it in GitHub Desktop.
Cartodb autocomplete search box example
<!DOCTYPE html>
<html>
<head>
<title>Cartodb search box autocomplete example | CartoDB.js</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" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.core.js"></script>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.11/themes/css/cartodb.css" />
<style>
.ui-autocomplete-loading {
background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
}
</style>
<script>
function initAutocomplete() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
var sql = cartodb.SQL({ user: 'documentation' });
$( ".cartodb-searchbox .text" ).autocomplete({
source: function( request, response ) {
var s
sql.execute("select adm0name, name from ne_10m_populated_places_simple_1 where name ilike '" + request.term + "%'").done(function(data) {
response(data.rows.map(function(r) {
return {
label: r.name + "," + r.adm0name,
value: r.name
}
})
)
})
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value :
"Nothing selected, input was " + this.value );
}
});
};
</script>
</head>
<body>
<div id="map"></div>
<!-- include google maps library *before* load cartodb.js -->
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.uncompressed.js"></script>
<script>
function main() {
var map;
// create google maps map
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(43, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
// create layer and add to the map, then add some intera
cartodb.createLayer(map, 'http://documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json')
.addTo(map)
.on('done', function(layer) {
var v = cdb.vis.Overlay.create('search', map.viz, {})
v.show();
$('#map').append(v.render().el);
initAutocomplete();
})
.on('error', function() {
cartodb.log.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