Error arises after navigating to a new page due to Google Maps DomListener

I seem to be facing an issue with the dom listener situated at the end of my Google Maps function in an external JavaScript file, resulting in a "google is not defined" console error. This error occurs when I navigate to another HTML page on my website that also accesses the script from the external file. I suspect that placing the DomListener outside the function is causing this problem, but moving it inside the function prevents the function from working properly. Can anyone guide me on the correct placement of this DomListener to resolve the error? Below is the snippet of my code.

Thank you for your assistance,

Anthony

function validateEmail() {
var email = form.emailaddress.value;
if (form.firstname.value == "") { 
document.getElementById("emailInvalid").style.visibility = "visible";   
return false;
} return true;
}

function initialize() {
  var myLatlng1 = new google.maps.LatLng(-25.363882,150.044922);
  var myLatlng2 = new google.maps.LatLng(-25.363882,152.044922);
  var mapOptions = {
    zoom: 6,
    center: myLatlng1
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var contentString1 = 'Mott Park'
  var contentString2 = 'Kilpa Park'

  var infowindow = new google.maps.InfoWindow({
    });

  var marker1 = new google.maps.Marker({
  position: myLatlng1,
  map: map,
  title: 'Park'
  });
  var marker2 = new google.maps.Marker({
      position: myLatlng2,
      map: map,
      title: 'Water'
  });
    google.maps.event.addListener(marker1, 'click', function initialize() {
        infowindow.close();
        infowindow.setContent(contentString1);
        infowindow.open(map, marker1);

  });
    google.maps.event.addListener(marker2, 'click', function initialize() {
       infowindow.close();
       infowindow.setContent(contentString2);
       infowindow.open(map, marker2);
  });
}
google.maps.event.addDomListener(window, 'load', initialize); 

Answer №1

There appears to be a potential duplicate:

"google is not defined" issue when utilizing Google Maps V3 in Firefox remotely

I believe it could be related to the positioning matter discussed by Diego Pino in the corresponding thread.

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

Could this be a substance made of pure white energy?

It appears that the default color for SpriteMaterial is 0xFFFFFF, which indicates no color. However, if I change the color property of the material to say 0xFF0000, the sprite will display a red tint. If I desire a white tint instead, how can this be achi ...

JavaScript: Converting an array of strings into an array of objects with proper formatting

After scanning barcodes, I have an array of strings that currently contains the following data: var array = ['NEW', '1111', 'serial1', 'serial2, 'NEW', '2222', 'serial3', 'serial4'] ...

The object FormData cannot be instantiated as a constructor

I'm currently facing an issue while trying to create an AJAX request to upload an image. The problem arises when I attempt to initialize the FormData object. The error message in my console states: "dataForm is not a constructor". Any ideas on how I ...

Alert from module (from ./node_modules/laravel-mix/node_modules/postcss-loader/dist/cjs.js):

I'm facing an issue while trying to set up the laravel breeze package for default authentication in my Laravel 9 project. After running npm run dev, I encountered a Laravel Mix error. WARNING in ./resources/css/app.css (./node_modules/css-loader/dis ...

Exploring the process of retrieving a pre-defined array within AngularJS

I'm encountering a challenge with accessing the array element in AngularJS. Here is the array: $scope.salesentry = [ { sales_id:'', product_id: '', product_category_id:'', sale_qty:nul ...

Is Node.js + Express capable of managing multiple requests simultaneously using just one CPU core?

In the midst of developing an API using nodejs, express, and mongodb within a Docker container. Currently, the Docker setup is operational on a DigitalOcean droplet featuring 512MB of RAM and 1 cpu core. From my research, I've learned that nodejs op ...

Incorporating an external JavaScript file into an AngularJS project

index.html: <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> <script src="./assets/namesController.js"></script> <body ng-app="myApp"> < ...

Are you in the business of building JavaScript hubs?

I have a unique setup where my express server is in charge of handling all routing and session functionalities. I've envisioned a system where logged-in users can connect to distinct "hubs" based on the location of each hub. My idea was to treat each ...

Tips for waiting on image loading in canvas

My challenge involves interacting with the image loaded on a canvas. However, I am uncertain about how to handle waiting for the image to load before starting interactions with it in canvas tests. Using driver.sleep() is not a reliable solution. Here is ...

How can I check if the VPN is turned off in a React application?

import React from "react"; import { Offline, Online } from "react-detect-offline"; export default function DropDown() { return ( <> <Online>Only displayed when connected to the internet</Online> <Offline ...

Flatbuffers does not exist in this context

Currently, I am working on a nodeJs application that involves the use of Google Flat Buffer. After installing flatc on my MacBook Pro, I compiled the schema below: namespace MyAlcoholist; table Drink { drink_type_name: string; drink_company_name: stri ...

Ensure that all content is completely loaded before displaying it using Angular 2

As an Angular developer, I am facing a challenge in my component where I am generating an image through a service HTTP call. Unfortunately, the image generation process takes longer than the site load time, causing the image to not appear immediately on th ...

How can a JavaScript function be assigned as a parameter to be used in conjunction with another function?

Currently, I am delving into the realm of JavaScript and there is one concept that has me a bit puzzled - passing functions as parameters to other functions. I grasp the idea in theory, but I am struggling to see its practical application. So, my query is ...

The array within the JSON object holds vital information [Typescript]

I have some data stored in an Excel file that I want to import into my database. The first step was exporting the file as a CSV and then parsing it into a JSON object. fname,lname,phone Terry,Doe,[123456789] Jane,Doe,[123456788, 123456787] Upon convertin ...

The event was triggered, however, some sections of the code did not run

I am currently working on a project called lan-info on GitHub. Here is the code snippet that I am using: let arpSweepCommand = './arpSweep.sh'; app.get('/arp', (req, res) => { console.log('we have a working signal!'); ...

Using ng-include destroys the styling of the dropdown menu in a bootstrap ul li format

Greetings! I am attempting to replicate the following code snippet that creates a basic dropdown menu using bootstrap: <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="fal ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

Experiencing trouble with calculating the total value of an array of objects due to NaN errors

I've been working on developing this web application in VUE.js, but I'm facing an issue with a specific function where I am attempting to sum the values of each object within an array. This is resulting in a NaN (Not a Number) error. Let's t ...

Looking for a seamless way to convert jsp code to html using sightly in AEM 6.1?

I need help transforming JSP code to HTML using Sightly in AEM. In the JSP code, we have a scriptlet that stores a value from the dialog into a JSP variable called "coltype" using the code pageContext.setAttribute("coltype", xssAPI.filterHTML(properties. ...

Using default JavaScriptSerializer to bind DateTime to knockout view model

Recently, I started using knockout and encountered a problem with DateTime Serialization and Deserialization when using the JavaScriptSerializer. I modified the gifts model in Steve's koListEditor example from his blog to include a new field for Modi ...