112 lines
3.4 KiB
HTML
112 lines
3.4 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Feature over/out | CARTO</title>
|
|
<meta name="viewport" content="initial-scale=1.0">
|
|
<meta charset="utf-8">
|
|
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Open+Sans:300,400,600" rel="stylesheet">
|
|
<!-- Include Google Maps -->
|
|
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDpVNTQI60ossApFzZ3dwSMZ1LcxOTY-rI&v=3.35"></script>
|
|
<!-- Include CARTO.js -->
|
|
<script src="../../../dist/public/carto.js"></script>
|
|
<link href="../style.css" rel="stylesheet">
|
|
</head>
|
|
|
|
<body>
|
|
<div id="map">
|
|
</div>
|
|
<!-- Description -->
|
|
<aside class="toolbox">
|
|
<div class="box">
|
|
<header>
|
|
<h1>Detect feature over/out</h1>
|
|
<button class="github-logo js-source-link"></button>
|
|
</header>
|
|
<section>
|
|
<p class="description open-sans">Interact with the features on the over/out event.</p>
|
|
<div class="separator"></div>
|
|
<section class="usage">
|
|
<header>USAGE</header>
|
|
<p class="open-sans">Move the mouse over the markers</p>
|
|
</section>
|
|
<div id="controls">
|
|
<div id="info"></div>
|
|
</div>
|
|
</section>
|
|
<footer class="js-footer"></footer>
|
|
</div>
|
|
</aside>
|
|
|
|
<script>
|
|
var map = new google.maps.Map(document.getElementById('map'), {
|
|
center: { lat: 30, lng: 0 },
|
|
zoom: 3,
|
|
fullscreenControl: false,
|
|
gestureHandling: 'cooperative'
|
|
});
|
|
|
|
// Hide the map labels and geometry strokes
|
|
map.set('styles', [{
|
|
elementType: 'labels',
|
|
stylers: [{ visibility: 'off' }]
|
|
}, {
|
|
elementType: 'geometry.stroke',
|
|
stylers: [{ visibility: 'off' }]
|
|
}]);
|
|
|
|
const client = new carto.Client({
|
|
apiKey: 'default_public',
|
|
username: 'cartojs-test'
|
|
});
|
|
|
|
const source = new carto.source.Dataset('ne_10m_populated_places_simple');
|
|
const style = new carto.style.CartoCSS(`
|
|
#layer {
|
|
marker-width: 10;
|
|
marker-fill: #EE4D5A;
|
|
marker-line-color: #FFFFFF;
|
|
}
|
|
`);
|
|
const layer = new carto.layer.Layer(source, style, {
|
|
featureOverColumns: ['name', 'pop_max']
|
|
});
|
|
|
|
client.addLayer(layer);
|
|
map.overlayMapTypes.push(client.getGoogleMapsMapType(map));
|
|
|
|
layer.on('featureOver', featureEvent => {
|
|
const content = `
|
|
<h3>${featureEvent.data.name.toUpperCase()}</h3>
|
|
<p class="open-sans">${featureEvent.data.pop_max} <small>max inhabitants</small></p>
|
|
`;
|
|
|
|
document.getElementById('info').innerHTML = content;
|
|
featureVisible = true;
|
|
});
|
|
|
|
layer.on('featureOut', featureEvent => {
|
|
hideInfo();
|
|
});
|
|
|
|
function debounce(func, wait, immediate) {
|
|
var timeout;
|
|
return function () {
|
|
var context = this, args = arguments;
|
|
var later = function () {
|
|
timeout = null;
|
|
if (!immediate) func.apply(context, args);
|
|
};
|
|
var callNow = immediate && !timeout;
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(later, wait);
|
|
if (callNow) func.apply(context, args);
|
|
};
|
|
};
|
|
|
|
const hideInfo = debounce(function () {
|
|
document.getElementById('info').innerHTML = '';
|
|
}, 500);
|
|
</script>
|
|
</body>
|
|
</html>
|