Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 1912

Joomla! 5.x Coding • Re: Issues using Google Maps with Joomla?

$
0
0
I can't reproduce the issue. But maybe myscript should be loaded before callmapscript? If so, you should make it a dependency of callmapscript.
What I have done now is the following changes:

joomla.asset file:

Code:

{  "$schema": "https://developer.joomla.org/schemas/json-schema/web_assets.json",  "name": "Gardentemplate",  "version": "4.0.0",  "description": "This file contains assets used by gardentemplate",  "license": "GPL-2.0-or-later",  "assets": [    {      "name": "mystyle",      "type": "style",      "uri": "style.css"    },    {      "name": "myscript",      "type": "script",      "uri": "script.js","attributes": {        "async": true      }    }  ]}
script.js:

Code:

const fetchedAdress = document.querySelector(".adress p").textContent;const cleanAdress = fetchedAdress.replace(/<[^>]+>/g, ''); // Removes HTML tagsconsole.log(cleanAdress);let map;let geocoder;async function initMap() {    // Request needed libraries.    //@ts-ignore    const { Map } = await google.maps.importLibrary("maps");    const { AdvancedMarkerElement } = await google.maps.importLibrary("marker");    geocoder = new google.maps.Geocoder();    geocoder.geocode({ 'address': cleanAdress }, function(results, status) {      if (status == 'OK') {      console.log(results);      var location = results[0].geometry.location;      const position = { lat: results[0].geometry.location.lat(), lng: results[0].geometry.location.lng() };            var mapOptions = {      zoom: 17,      center: position,      mapId: results[0].place_id,      }            // The map, centered at Uluru      map = new Map(document.getElementById("map"), mapOptions);            // The marker, positioned at Uluru      const marker = new AdvancedMarkerElement({      map: map,      position: position,      title: results[0].formatted_address,      });      } else {      console.log('Geocode was not successful for the following reason: ' + status);      }      });  }    initMap();
index.php:

Code:

$wa->useStyle('mystyle')->useScript('myscript');
index.php at the end of document:

Code:

<jdoc:include type="scripts" /><script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({    key: "AIzaSyAVcByTo_57_zc2iqRvVlJauObFA9Sub5s",    v: "weekly",  })</script>
What happens is map seems to work, BUT if you reload the page, OR go to contact form or news category, the map isn't displayed, and I get the following error in console:
Uncaught SyntaxError: redeclaration of var map
<anonymous> http://localhost/blomstradgard/template ... s?9aa240:1
So variable map is being declared again despite already being declared once, is how I interpret it. Do I need to declare this variable elsewhere?

Statistics: Posted by SocketPup — Wed May 15, 2024 7:24 am



Viewing all articles
Browse latest Browse all 1912

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>