Home / Widgets / zooming to user’s location
Duplicate Snippet

Embed Snippet on Your Site

zooming to user’s location

T

Code Preview
js
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 1. Find all images within the WPForms View
    const itemImages = document.querySelectorAll('.wpforms-view-entry img');
    itemImages.forEach(img => {
        img.style.cursor = 'pointer'; // Make it look clickable
        img.addEventListener('click', function() {
            // 2. Find the postcode text near the map marker icon
            const entry = this.closest('.wpforms-view-entry');
            const markerIcon = entry.querySelector('.fa-map-marker');
            if (markerIcon) {
                // Get the text content next to the icon (the postcode)
                const postCode = markerIcon.parentElement.innerText.trim();
                // 3. Trigger Map Zoom (Assumes your map uses a standard Google Maps instance)
                if (window.google && window.google.maps) {
                    const geocoder = new google.maps.Geocoder();
                    geocoder.geocode({ 'address': postCode }, function(results, status) {
                        if (status === 'OK' && results[0]) {
                            // Replace 'myMapInstance' with your actual map variable name
                            if (window.myMapInstance) {
                                window.myMapInstance.setCenter(results[0].geometry.location);
                                window.myMapInstance.setZoom(16);                    }
                        }                   
 });
                }
            }        
});
    });
});
</script>

Comments

Add a Comment