HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/RMourik/bassol.nl/CMS/DancingGoat/Scripts/Modules/ScrollToMap.js
define(['domReady!', 'jQuery'], function (document, $) {

    var google = window.google,
        mapObject,

        // There is no simple way to access map object directly,
        // because the object is a global object and its name
        // is the map div wrapper's IDentifier        
        loadMapObject = function () {
            var mapObjectIdentifier = $('.js-map-wrapper')
                .children('div')
                .not('div[class^="Webpart"]')
                .attr('id');

            mapObject = window[mapObjectIdentifier];
        },

        scrollToLocation = function (location) {
            $("html, body").animate({ scrollTop: $(".map-title").offset().top }, 400);
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': location }, scrollToMapPosition);
        },

        scrollToMapPosition = function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                mapObject.setCenter(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));
                mapObject.setZoom(16);
            }
        };       


    $('.js-scroll-to-map').each(function (elementPosition, element) {
        var $element = $(element);

        $element.on('click', (function ($el) {
            return function () {
                if (!mapObject) {
                    loadMapObject();
                }
                var elAddress = $el.data().address;
                scrollToLocation(elAddress);
            }
        }($element)));
    });
});