- This topic has 5 replies, 2 voices, and was last updated 2 years, 3 months ago by
Support. This post has been viewed 1457 times
-
AuthorPosts
-
April 27, 2024 at 12:36 am #10044765
wael
ParticipantI’m having an issue with JavaScript. Google Maps isn’t loading properly. there’s a message error: google maps did not load correctly on this page. Thank you for your help.
April 27, 2024 at 1:12 am #10044766Support
KeymasterHello, the error is that you need to create an API Key.
DOCS: https://www.okler.net/previews/porto/docs/#google_maps
Get an API Key: https://developers.google.com/maps/documentation/javascript/get-api-key
In the JS code at the bottom of the page, where it says “YOUR_API_KEY” you provide that Key there.
April 27, 2024 at 1:13 am #10044767Support
KeymasterApril 27, 2024 at 1:14 am #10044768Support
KeymasterUpdated JS code. (bottom of the HTML page)
/* Map Markers: - Get an API Key: https://developers.google.com/maps/documentation/javascript/get-api-key - Generate Map Id: https://console.cloud.google.com/google/maps-apis/studio/maps - Use https://www.latlong.net/convert-address-to-lat-long.html to get Latitude and Longitude of a specific address */ (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: "YOUR_API_KEY", v: "weekly"}); async function initMap() { const { Map, InfoWindow } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary( "marker", ); const map = new Map(document.getElementById("googlemaps"), { zoom: 14, center: { lat: -37.817240, lng: 144.955820 }, mapId: "YOUR_MAP_API_ID", }); const markers = [ { position: { lat: -37.817240, lng: 144.955820 }, title: "Office 1<br>Melbourne, 121 King St, Australia<br>Phone: 123-456-1234", } ]; const infoWindow = new InfoWindow(); markers.forEach(({ position, title }, i) => { const pin = new PinElement({ background: "#e36159", borderColor: "#e36159", glyphColor: "#FFF", }); const marker = new AdvancedMarkerElement({ position, map, title:${title}, content: pin.element, }); marker.addListener("click", ({ domEvent, latLng }) => { const { target } = domEvent; infoWindow.close(); infoWindow.setContent(marker.title); infoWindow.open(marker.map, marker); }); }); } initMap();
April 27, 2024 at 1:37 am #10044769wael
ParticipantI copied and pasted this code; there’s an error in it according to VS Code:
/*
Map Markers:
– Get an API Key: https://developers.google.com/maps/documentation/javascript/get-api-key
– Generate Map Id: https://console.cloud.google.com/google/maps-apis/studio/maps
– Use https://www.latlong.net/convert-address-to-lat-long.html to get Latitude and Longitude of a specific address
*/
(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: “YOUR_API_KEY”, v: “weekly”});async function initMap() {
const { Map, InfoWindow } = await google.maps.importLibrary(“maps”);
const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary(
“marker”,
);
const map = new Map(document.getElementById(“googlemaps”), {
zoom: 14,
center: { lat: -37.817240, lng: 144.955820 },
mapId: “YOUR_MAP_API_ID”,
});
const markers = [
{
position: { lat: -37.817240, lng: 144.955820 },
title: “Office 1<br>Melbourne, 121 King St, Australia<br>Phone: 123-456-1234”,
}
];const infoWindow = new InfoWindow();
markers.forEach(({ position, title }, i) => {
const pin = new PinElement({
background: “#e36159”,
borderColor: “#e36159”,
glyphColor: “#FFF”,
});
const marker = new AdvancedMarkerElement({
position,
map,
title:
${title}
,
content: pin.element,
});marker.addListener(“click”, ({ domEvent, latLng }) => {
const { target } = domEvent;infoWindow.close();
infoWindow.setContent(marker.title);
infoWindow.open(marker.map, marker);
});
});}
initMap();
April 27, 2024 at 1:47 am #10044770Support
KeymasterThe code doesn’t have any errors, it’s provided by Google.
Did you create your API Key?
Note it’s not 100% free, Google asks for a credit card and payment method to use that service.
Check their documentation in the link:
https://developers.google.com/maps/documentation/javascript/get-api-key
-
AuthorPosts
This topic is marked as "RESOLVED" and can not rceive new replies.
