// Google Maps Scripts const googleMaps = function () { let map = null; let currentLat = null; let currentLng = null; let place = null; let placeInfo = null; /** * * @param callback */ const handleInitPlace = function (callback) { let autoComplete = new google.maps.places.Autocomplete(document.getElementById('address')); let addressInputElement = $('#address'); addressInputElement.on('focus', function () { let pacContainer = $('.pac-container'); $(addressInputElement.parent()).append(pacContainer); }); autoComplete.addListener('place_changed', function () { place = autoComplete.getPlace(); currentLat = place.geometry.location.lat(); currentLng = place.geometry.location.lng(); placeInfo = handleGetPlaceInfo(); if (callback && typeof (callback) === 'function') { return callback.call(this, place); } }); }; /** * * @param position * @param zoom * @param options * @returns {*} */ const handleInitMap = function(position, zoom, options) { let config = {center: position, zoom: zoom}; if (options !== undefined) { for (let key in options) { config[key] = options; } } map = new google.maps.Map(document.getElementById('map'), config); return map; }; /** * * @param position * @returns {boolean|*} */ const handleSetMarker = function(position){ if (position === undefined || map === null) { return false; } return new google.maps.Marker({ position: position, map: map }); }; /** * * @returns {{}} */ const handleGetPlaceInfo = function () { let temp = {}; let $adr = $('
'); $adr.append(place.adr_address); temp['address'] = place.name; temp['vicinity'] = place.vicinity; temp['formatted_address'] = place.formatted_address; temp['postal_code'] = $adr.find('.postal-code').text(); temp['city'] = $adr.find('.locality').text(); return temp; }; const handleInit = function () { const $map = $('#map'); if (!$map.length) { return; } const lat = $map.data('lat'); const lng = $map.data('lng'); const gps = {lat: parseFloat(lat), lng: parseFloat(lng)}; handleInitMap(gps, 18, { disableDefaultUI: true }); // Add new marker. handleSetMarker(gps); }; /** * * @returns {null} */ const handleGetPlace = function () { return placeInfo; }; /** * * @returns {{lng: null, lat: null}} */ const handleGetGPS = function () { return {lat: currentLat, lng: currentLng}; }; /** * * @returns {*} */ const handleGetData = function(){ let temp = handleGetPlace(); if (temp !== null) { const GPS = handleGetGPS(); temp.lat = GPS.lat; temp.lng = GPS.lng; } return temp; }; return { init: function () { return handleInit(); }, /** * * @param callback * @returns {googleMaps} */ initPlace: function (callback) { handleInitPlace(callback); return this; }, /** * * @param position * @param zoom * @param options * @returns {*} */ initMap: function(position, zoom, options) { return handleInitMap(position, zoom, options); }, /** * * @param position * @returns {boolean|*} */ setMarker: function(position) { return handleSetMarker(position); }, /** * * @returns {*} */ getPlaceInfo: function () { return handleGetPlace(); }, /** * * @returns {{lng: null, lat: null}} */ getGPS: function () { return handleGetGPS(); }, /** * * @returns {*} */ getData: function() { return handleGetData(); } } }();