Skip to content

Instantly share code, notes, and snippets.

@danswick
Last active November 2, 2017 04:01
Show Gist options
  • Select an option

  • Save danswick/9326da24d87c1f5c18535754fcd41c99 to your computer and use it in GitHub Desktop.

Select an option

Save danswick/9326da24d87c1f5c18535754fcd41c99 to your computer and use it in GitHub Desktop.
Update a popup's content in Mapbox.js

Use setPopupContent() to update a popup periodically.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Display marker tooltip on load</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v2.4.0/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v2.4.0/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoiZGFuc3dpY2siLCJhIjoiY2l1dTUzcmgxMDJ0djJ0b2VhY2sxNXBiMyJ9.25Qs4HNEkHubd4_Awbd8Og';
var map = L.mapbox.map('map');
map.setView([51.50068,-0.12450], 19);
L.mapbox.styleLayer('mapbox://styles/mapbox/streets-v9').addTo(map);
// Build a marker from a simple GeoJSON object:
var marker = L.mapbox.featureLayer({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-0.12458,51.50068]
},
properties: {
title: 'Hello world!',
'marker-color': '#f86767'
}
})
.on('ready', run)
.addTo(map);
// Iterate over the featureLayer we've called "marker"
// and open its popup and update the popup content
function run() {
marker.eachLayer(function(l) {
l.openPopup();
var d = new Date(),
h = ("0" + d.getHours()).slice(-2),
m = ("0" + d.getMinutes()).slice(-2),
s = ("0" + d.getSeconds()).slice(-2);
var currentTime = "The current time is: </br><strong>" + h + ":" + m + ":" + s + "</strong>";
l.setPopupContent(currentTime);
});
}
// update the popup every second with the new time
window.setInterval(function() {
run();
}, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment