Vue initialization encounters an issue with starting 'marker-animate-unobtrusive' package

I am encountering an issue with marker-animate-unobtrusive and keep receiving the following error:

https://i.sstatic.net/Y69xK.png

While browsing through SO, I came across a post discussing the importance of requiring the file after Google has loaded. However, I am unsure how to implement this. In my component, I currently have the following:

import SlidingMarker from 'marker-animate-unobtrusive'

Within my mounted method, I have included:

SlidingMarker.initializeGlobally()

Any assistance on this matter would be greatly appreciated.

Answer №1

It is common to encounter this error when using SlidingMarker which extends the google.maps.Marker class. In order for the GoogleMaps JavaScript API library to function properly, it needs to be loaded first. One solution is to include a reference in the index.html file:

<script src="https://maps.googleapis.com/maps/api/js?key=--KEY-GOES-HERE--"></script>   

Alternatively, you can use an async JavaScript loader like scriptjs. Here is an example of loading the GoogleMaps JavaScript API and the marker-animate-unobtrusive module:

created: function(){
  get("https://maps.googleapis.com/maps/api/js?key=", () => {

    const SlidingMarker = require('marker-animate-unobtrusive')
    SlidingMarker.initializeGlobally()

    const map = new google.maps.Map(document.getElementById('map'), this.mapOptions);

    const marker = new SlidingMarker({
        position: this.mapOptions.center,
        map: map,
        title: 'Im sliding marker'
    });
 });

}

Here is a demo that you can refer to

Update

By using the vue-google-maps library, you can integrate the marker-animate-unobtrusive plugin as follows:

<template>
  <div>
    <GmapMap :center="center" :zoom="zoom" ref="mapRef"></GmapMap>
  </div>
</template>

<script>

/* global google */

export default {
  data() {
    return {
      zoom: 12,
      center: { lat: 51.5287718, lng: -0.2416804 },
    };
  },
  mounted: function() {
     this.$refs.mapRef.$mapPromise.then(() => {
        this.initSlidingMarker(this.$refs.mapRef.$mapObject)
    })
  },
  methods: {
    initSlidingMarker(map){
       const SlidingMarker = require('marker-animate-unobtrusive')
       SlidingMarker.initializeGlobally()


       const marker = new SlidingMarker({
            position: map.getCenter(),
            map: map,
            title: 'Im sliding marker'
       });

       google.maps.event.addListener(map, 'click', (event) => {
          marker.setDuration(1000);
          marker.setEasing('linear');
          marker.setPosition(event.latLng);
       });
    }
  }
}
</script>

<style>
.vue-map-container {
  height: 640px;
}
</style>

Answer №2

To resolve the issue, you can resolve it by installing the npm package google.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to refresh updated .js files directly from the browser console?

Is it possible to use RequireJS 2 to dynamically reload a recently updated .js file? Let me explain my scenario: I have a Backbone.js object named Foo with a function called Bar that currently displays an alert with "abc" when called; In my webpage, I ex ...

What is the process for using the scrollIntoView function on an svg element like a rectangle?

I have a graph panel with an SVG. The nodes in the SVG are listed in a separate panel. I would like for when a node is clicked in the node list, the SVG will scroll to that specific node. Each node is represented by a rectangle shape. However, I noticed th ...

Transform a javascript object with class attributes into a simple object while keeping the methods

I am seeking a way to convert an instance of a class into a plain object, while retaining both methods and inherited properties. Here is an example scenario: class Human { height: number; weight: number; constructor() { this.height = 1 ...

Tips on invoking a mixin within a Jade template showcased in a Node/Express endpoint

I'm currently developing a component that I plan to use multiple times on a website through a Jade template. This is how my route is set up: router.get('/', function(req, res, next) { res.render('indicators',{category:"", num ...

Exploring the World of GiantBomb APIs

I have successfully created an account and obtained my API key. I am looking to implement a basic search functionality on my webpage, where users can enter a search query and click a button to display the game title and image. You can find more informatio ...

Leveraging Next.js ISR to pass extra information to the getStaticProps function from the getStaticPaths

Inside SingleBlogPost.jsx, the code for generating blog pages by their slug is as follows: export async function getStaticPaths() { const res = await fetch("http://localhost:1337/api/posts"); let { data } = await res.json(); const paths = data.map(( ...

What is the point of utilizing angular.extend in this situation?

After inheriting a codebase, I stumbled upon the following code snippet (with some parameters simplified and anonymized): /** * @param {float} num1 * @param {string} str1 * @param {string} str2 * @param {boolean} flag & @return (object) */ sr ...

Is it possible to create a dynamic template in Angular using external sources?

My goal is to dynamically load HTML content using AJAX and then compile it, as it contains Angular directives. I have a specific class that includes methods for utilizing Angular outside the scope of an angular controller or directive: var AngularHelper ...

Tips for maximizing website performance on touch-enabled devices

When using a touch device such as an iPhone, iPad, or Android device, it can be challenging to accurately tap on small buttons with your finger. So far, there is no universal method in CSS media queries to detect touch devices. As a workaround, I check if ...

Mastering the use of npm and sails to create HTML-PDF files effortlessly

UPDATE: I am simplifying my question and will address any other concerns in separate posts if necessary. The initial post was too lengthy, hoping for a straightforward guide on utilizing sails to its fullest potential. Apologies. To begin with, my knowled ...

Is it possible to scroll by using the dragenter event?

Looking for a way to achieve incremental scroll up and scroll down using jQuery without jQuery UI? Here's the scenario - I have two divs: <div class="upper" style="height:35px;background-color:red;right:0;left:0;top:0;position:fixed;width:100%;z-i ...

What is the best way to structure a JSON data string for transmission from a WebView to JavaScript?

Seeking a solution for passing multiple values from an Android WebView to JavaScript. The challenge is that the string received in JS appears completely raw with control characters. The specific issue arises when sending the following string from Java: f ...

Adding a class-matched element with a corresponding ID

Despite finding similar questions, I am still unable to achieve the desired outcome. Here is a list: <ul> <li class="class1"></li> <li class="class2"></li> <li class="class3"></li> <li class="class4"&g ...

Having trouble uploading big files on your Windows system?

My challenge is to successfully upload a large file using Angular. Specifically, I am attempting to upload a Human Genome tar file, which is a minimum of 2.5gb in size. Strangely, when I try to upload the file from Linux using any browser such as Chrome or ...

I am facing issues with jQuery's live and livequery functions when trying to use them with a form that is being loaded dynamically through

Previously, I inquired about a related issue regarding attaching behavior to an element with jQuery after insertion. However, I have not yet found a solution. For clarity's sake, I am posing a new question with a different scenario. Below is the code ...

Customize the inline click event using jQuery

I have a navigation with links that resemble the following: <a id="navform" href="#" tabindex="-1" onclick="mojarra.ab(this,event,'action','@form','content');return false" class=" ...

Is it possible to organize MongoDB records that possess identical update timestamps?

My goal is to validate a route within my Express server using Supertest. This particular route retrieves data from a MongoDB, and the data is then sorted based on the updatedAt field. While attempting to test the order of the output, I encountered an issu ...

Is it feasible to create that specific character using Google Charts?

Quick question here. Can a Char be generated in Google Charts? Also, do you have any advice on how to do this? Chart1 The blank space under the chart is crucial. ...

``It seems like there was an error with WebComponents and NextJS - the hydration failed due to a mismatch between the initial UI and what was rendered on

I'm running into an issue with the following error message: Error: The initial UI doesn't match what was rendered on the server, leading to hydration failure. This problem occurs when I have a NextJS webpage that includes StencilJS web compone ...

Are Bootstrap Input groups inconsistent?

Hey there! I've been working on the sign-in example, but I seem to have hit a roadblock. In my local setup, the top image is what I see in my browser after running the code, while the desired layout that I found on the Bootstrap site is the one below ...