Examples
Practical code examples for common developer use cases.
Fly to a Location on External Element Click
A common pattern is building a sidebar or list where clicking an item flies the map to the corresponding feature. This example uses location IDs from the WordPress backend to match list items to map features.
<!-- HTML: list items with IDs matching location post IDs -->
<div>
<p class="map-office" id="map-office-578">Mount Vernon Office</p>
<p class="map-office" id="map-office-577">Puyallup Office</p>
</div>
<script>
window.mwm_v2.onload(() => {
window.mwm_v2.add_action('features:rendered', ({ map, features }) => {
document.querySelectorAll('.map-office').forEach(el => {
el.addEventListener('click', () => {
const id = parseInt(el.id.replace('map-office-', ''));
const feature = features.find(f => f.id === id);
if (feature) {
map.flyTo({ center: feature.data.location.coordinates, zoom: 12 });
}
});
});
});
});
</script>Run Code Only on a Specific Map
On pages with multiple maps, use config.id to target one:
Show and Hide Layers on Button Click
Attach a Native Map Event Listener
For interaction detail not available through the add_action API (such as which specific feature was clicked), attach listeners directly to the map object using the native library's event API:
Last updated