0

I am trying to utilize AdvancedMarkerView of Google Maps API with @googlemaps/js-api-loader in Nuxt3 in order to create a map similar to the one found here.
I attempted to use the example code for Advanced Markers found in @googlemaps/js-api-loader, but the markers are not showing up on the map.

Here is example code:
https://github.com/googlemaps/js-api-loader/tree/main/examples


Here is my code:

HTML:

<div id="canvas" ref="canvas" class="canvas"></div>

TypeScript - maps.vue:

import { Loader } from '@googlemaps/js-api-loader';

const map = ref<google.maps.Map>();
const canvas = ref<HTMLElement>();

const loader = new Loader({
   apiKey: *************,
   version: "weekly",
   libraries: ["places"],
});

const initMap = () => {
    loader.importLibrary('maps')
        .then(async ({Map}) => {
            map.value = new Map(canvas.value as HTMLElement, {
                center: { lat: 34.6937, lng: 135.5023 },
                zoom: 17,
                mapId: '**************',
            });
            const priceTag = document.createElement('div');
                priceTag.className = 'price-tag';
                priceTag.textContent = '$2.5M';
            const {AdvancedMarkerElement} = await loader.importLibrary('marker'); // Here is line186
            new AdvancedMarkerElement({
                map: map.value,
                position: { lat: 34.6937, lng: 135.5023 },
                content: priceTag,
            });
        })
        .catch((e) => {
            // do something
        });
}

onMounted(() => {
      initMap();
})

A warning message is displayed:

maps.vue:186 
Google Maps already loaded outside @googlemaps/js-api-loader.This may result in undesirable behavior as options and script parameters may not match.

How can I modify the code so that the marker shows up on the map?

PS:

I created a sample code on codesandbox.

https://codesandbox.io/s/interesting-sun-4xzhnf?file=/src/App.vue

moonris
  • 117
  • 1
  • 10
  • 1
    You must be loading the API somewhere else. But not in the code you shared. – MrUpsidown Jun 16 '23 at 11:59
  • The code that loads the API is not located elsewhere. I likely encountered this error because I called "loader.importLibrary()" twice. I would like to know how to prevent the error. – moonris Jun 16 '23 at 12:38

1 Answers1

0

I found my mistake. When I changed it not to use ref to load the map,
the marker was displayed on the map.

const map = new Map(canvas.value as HTMLElement, {...
new AdvancedMarkerElement({
             map,
             ...

However, the warning is still displayed, but I will try using the map for the time being.
Thank you very much.

moonris
  • 117
  • 1
  • 10
  • for some reason AdvancedMarkerElement doesn't work with Vue ref, confirm that. I also facing the warning after switching from deprecated `load` to `importLibrary`. Were you able to find out the reason? – Serg Jul 20 '23 at 11:42