Custom search bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Custom search 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://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" /> | |
</head> | |
<body> | |
<div id="map"></div> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script> | |
<script> | |
cdb.geo.ui.Search = cdb.geo.ui.Search.extend({ | |
_submit: function(ev) { | |
ev.preventDefault(); | |
var self = this; | |
var address = this.$('input.text').val() + ', British Columbia, Canada'; | |
// Show geocoder loader | |
this._showLoader(); | |
cdb.geo.geocoder.NOKIA.geocode(address, function(coords) { | |
if (coords.length>0) { | |
var validBBox = true; | |
// check bounding box is valid | |
if(!coords[0].boundingbox || coords[0].boundingbox.south == coords[0].boundingbox.north || | |
coords[0].boundingbox.east == coords[0].boundingbox.west) { | |
validBBox = false; | |
} | |
if (validBBox && coords[0].boundingbox) { | |
self.model.setBounds([ | |
[ | |
parseFloat(coords[0].boundingbox.south), | |
parseFloat(coords[0].boundingbox.west) | |
], | |
[ | |
parseFloat(coords[0].boundingbox.north), | |
parseFloat(coords[0].boundingbox.east) | |
] | |
]); | |
} else if (coords[0].lat && coords[0].lon) { | |
self.model.setCenter([coords[0].lat, coords[0].lon]); | |
self.model.setZoom(10); | |
} | |
} | |
// Hide geocoder loader | |
self._hideLoader(); | |
}); | |
} | |
}); | |
</script> | |
<script type="text/javascript"> | |
var layer; | |
function main() { | |
cartodb.createVis('map', 'http://team.cartodb.com/api/v2/viz/2e8df4e6-347f-11e5-b38d-0e853d047bba/viz.json', { searchControl: true }) | |
// .addTo(map) | |
.on('done', function() { | |
}).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