Hi people, im new devolping in shopify, I need some features of google maps API so Im doing the import in a script tag.
{% render "app_snippet" %}
{% schema %}
{
"name": "Hello World",
"target": "section",
"stylesheet": "customers.css",
"javascript":"customers.js",
"settings": [
{ "label": "Color", "id": "color", "type": "color", "default": "#000000" }
]
}
{% endschema %}
In other file i have the request to my API.
const getLocations = () => {
let map = new google.maps.Map(document.querySelector('.map_canvas'), {
zoom: 14,
center: new google.maps.LatLng(19.4326296, -99.1331785),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
console.log(map);
}
the problem is that my page is loaded before the google’s script and when i want to use the google variable doesnt exist.
If i dont set the property async or defer in the script tag ill get the next error by liquid when i try to push my changes.
So i have to use any of the 2 properties in the tag.
Missing async or defer attribute on script tag at blocks/customers.liquid:1
I tried to do the import since javascript but its not possible, just works with internal scripts.
I found the next possible solution but i dont think its the best way and im not sure how to do it.
1: Move the inline script to an external Javascript file
If you create an external JS file, you can load the script using the defer keyword as all deferred scripts execute in the order that they were called once the DOM is ready. As an added bonus, you wouldn't need to wrap your function with the jQuery(document).ready as you would already know that the document is ready when the script executes.
I suppose there should be a bether way.
THANKS FOR YOUR ANSWER.