Unable to render Google map on Vue CLI component

I am currently using the google map api to develop a basic application with vue.js. Interestingly, when I utilize a simple html and javascript setup with the api key, everything runs smoothly. However, once I transition the same process to vue, the map fails to appear without any error messages in the console. It's puzzling because I can't pinpoint what mistake I might be making.

Within the project's index.html file, I embed the api url in this manner:

<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=MY_API_KEY"></script>

Vue component

<template>
 <div>
  <h3>The map will be displayed here</h3>
<div ref="map" id="map"></div>
 <button @click="getMap">Show the map</button>
</div>
getMap() {

    const uluru = { lat: -25.344, lng: 131.036 };
    const map = new window.google.maps.Map(this.$refs['map'],{

         zoom: 15,
         center: uluru,
         mapTypeID: window.google.maps.MapTypeId.ROADMAP
       }

return map;
}

It is necessary to prefix window before 'google' while calling the google map object in vue cli, or else an error occurs.

new window.google.maps.Map()...instead of new google.maps.Map()

Despite following the code as shown above, no map appears on the screen and no errors are indicated in the console. Where could I possibly be going wrong?

Answer №1

Begin by verifying that the Api key you entered is accurate, rather than using key=MY_API_KEY. Next, include some CSS styles such as #map {height: 450px;}, since the default height for

<div ref="map" id="map"></div>
is height = 0

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

Developing an ASP application using the MVP pattern to return JSON data can be transformed into a S

I’m looking to incorporate the following code into a sails js Controller public JsonResult GetEvents() { //Using MyDatabaseEntities as our entity datacontext (refer to Step 4) using (MyDatabaseEntities dc = new MyDatabaseEntities()) { ...

Hiding a Component: Achieving Smooth Behavior with Timer and onPress

My goal is to create a user interface where tapping the screen displays a <TouchableWithoutFeedback> component, which should automatically disappear after 4 seconds. Additionally, I want the user to be able to tap on the displayed component to hide ...

React Ant Design: Encountered a warning for 2 elements having non-unique properties

I have incorporated the Carousel component, with each one containing a different component. One contains a form, while the other includes an external library component called react-input-otp. https://codesandbox.io/s/pensive-einstein-uucvm?fontsize=14& ...

Removing a pin from google maps using a personalized delete button

I have encountered an issue while attempting to remove a marker from Google Maps using a custom delete button within the info window. Even though I have successfully added the button and necessary information, it seems that the function responsible for del ...

Can state be controlled by both the parent and children within the same element?

I am facing a situation where I have a component that requires updating the value from its parent, but also needs to handle user input changes without having to pass them back to the parent. See this scenario in action with this example: https://codesandbo ...

I need to change a website into a string so that I can analyze it with javascript. How can I do this?

Currently, I am in the process of creating a website for my video game servers. The admin tool we use outputs the current server status in a .json format as a large text string to this specific URL: My goal is to retrieve the entire text string from the p ...

The database is not being updated with the new values

Is it possible to retrieve data from a database and modify it using AJAX? I tried changing the input, but AJAX keeps sending the original data stored in the database. <script> function showMore(str) { var xhttp; if (str.length == ...

Converting a date from PHP to JavaScript

My PHP code generates the following date: <?php date_default_timezone_set('Europe/London'); echo date('D M d Y H:i:s O'); ?> Additionally, I have a JavaScript/jQuery script that displays this date on the website: setInterval( ...

Trouble displaying AngularJS $scope.data in the HTML code

I'm experiencing an issue where the data received from a POST request is being logged in the console, but is not displaying on the HTML page. Despite having a controller set up, the {{user}} variable is not appearing on the HTML page. While I can se ...

Using Node JS, how to pass a variable length array to a function?

Is there a way to dynamically call an addon function with varying argument lengths? I capture user input in a variable like this: Uinput = [5,3,2]; Now, I want to pass these numbers as arguments to my addon function like this: addon.myaddon(5,3,2); I n ...

"Implementing Notifications in Mongoose: A Step-by-Step Guide

My current user schema is set up as follows: const Schema = mongoose.Schema; const bcrypt = require("bcryptjs"); const userSchema = new Schema( { email: { type: String, required: true, index: { unique: true } }, ...

Restrict certain sections of the website to authorized users only

In my game project, players are presented with two options: to play the game itself or to view global and local highscores. I have implemented a signup and login system where upon successful login, users can choose among playing the game, checking highscor ...

Maintain connection on a specific route rather than across all routes within the router-view

When specifying keep-alive to router-view like this: <keep-alive> <router-view></router-view> </keep-alive> All routes are cached and reloaded whenever that route is revisited. I am interested in having the option to specify ...

Creating a responsive image within a panel using Bootstrap

I've been struggling to make a responsive image fit inside a panel while maintaining its aspect ratio and ensuring none of it gets cut off. I've attempted various CSS tweaks with no success. My setup involves Bootstrap along with React.js using r ...

"Looking to update the main page content in Vuejs/Nuxt2 - what's the best way to

Currently, I am including my MainPage.vue from the components folder to display in my index.vue by utilizing the code below. <template> <div> <MainPage/> </div> </template> Now, my query is: How can I implement a butt ...

Utilizing the loop counter within an Array

Currently, I am attempting to iterate through numbers 1 to 21 and then utilize those numbers in order to obtain an Array of Strings like ['e1.wkh',...'e21.wkh']. However, at the moment I am only receiving the value ['e21.wkh'] ...

Annoying jQuery animation error: struggling to animate smoothly and revert back. Callback function conundrum?!

I'm completely lost with what I have accomplished. My goal was to create an animation where an element slides in from a certain position and then slides back when another element is clicked. To achieve this, I included the second event within the call ...

Minimize the amount of space used in GridLayout

My component has a Grid Layout with 1 column specified as auto, auto. I have placed 2 Items within this Grid, each taking up the full space of the column. However, due to the large size of the items, I have reduced them using transform: scale(0.7);. The i ...

HTML - implementing a login system without the use of PHP

While I am aware that the answer may lean towards being negative, I am currently in the process of developing a series of web pages for an IST assignment in Year 9. Unfortunately, the web page cannot be hosted and our assessor lacks the expertise to utiliz ...