I'm having trouble getting the infoWindow to function properly

As someone who is new to JavaScript, I recently came across a question on Google Maps JS API v3 - Simple Multiple Marker Example and attempted to implement the solution provided by Daniel Vassallo.

var Australia = new google.maps.LatLng(-27.16881, 132.72259);

var placesIhaveBeen = [
    ['Aalborg Airport Flight leaves at:xxxxxx', 57.04807, 9.91857]
    ['Copenhagen Airport Arrives leaves at:xxxxxx', 55.61541, 12.64885]
    ['Dubai Airport Flight Arrives at: xxxxx', 25.25278, 55.36444]
    ['Sydney Airport Flight Arrives at:xxxxxx', -33.93947, 151.17522]];

var markers = [];
var iterator = 0;
var map;
var infowindows = new google.maps.InfoWindow();
var marker, i;

function initialize()
{
    var mapOptions = {
        zoom: 4,
        center: Australia,
        mapTypeControl: false

    };
    map = new google.maps.Map(document.getElementById("googleMap-Canvas"),
        mapOptions);

    //flight path Polyline
    var flightPlanCoordinates = [
    new google.maps.LatLng(57.04807, 9.91857),
    new google.maps.LatLng(55.61541, 12.64885),
    new google.maps.LatLng(25.25278, 55.36444),
    new google.maps.LatLng(-33.93947, 151.17522)
    ];
    var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });

    flightPath.setMap(map);
    //end of flight path polyline

    for (i = 0; i < placesIhaveBeen.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(placesIhaveBeen[i][1], placesIhaveBeen[i][2], placesIhaveBeen[i][3], placesIhaveBeen[i][4]),
            map: map
        });

        google.maps.eve.addDomListener(marker, 'click', (function (marker, i) {
            return function () {
                infowindow.setContent(placesIhaveBeen[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }
}

google.maps.event.addDomListener(window, 'load', initialize);

That represents my entire Script, including some flight paths. I am seeking assistance in showcasing both the markers and the polyline simultaneously, as my current implementation seems to be falling short. Any help would be greatly appreciated :)

Answer №1

You've made a few mistakes:

There's a missing comma in the array definition of placesIhaveBeen:

var placesIhaveBeen = [
    ['Aalborg Airport Flight leaves at:xxxxxx', 57.04807, 9.91857],
    ['Copenhagen Airport Arrives leaves at:xxxxxx', 55.61541, 12.64885],
    ['Dubai Airport Flight Arrives at: xxxxx', 25.25278, 55.36444],
    ['Sydney Airport Flight Arrives at:xxxxxx', -33.93947, 151.17522]
];

infowindow is not defined, so the line

var infowindows = new google.maps.InfoWindow();

should be

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

There is also a typo in google.maps.eve.addDomListener. It should be google.maps.event.addDomListener.

Check out the example on jsbin. The map container is named differently.

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

Tips for restricting additional input when maximum length is reached in an Angular app

In my Angular 6 application, I am working on implementing a directive that prevents users from typing additional characters in an input field. However, I want to allow certain non-data input keys such as tab, delete, and backspace. Currently, I have an if ...

Retrieve both the key and corresponding value from a JSON object

I am attempting to extract the key and value pairs from a JSON dataset. $.get(url, function (data) { console.log(data); if (data.Body != null) { console.log(data.Body); } }); These are my current logs: { $id: "1", Exceptions ...

React application experiencing Unhandled Promise Rejection (RangeError): Call stack size limit reached

For my first form in ReactJS, I used a functional app and successfully implemented basic validations. I've also created a function to handle form submission to the backend. But upon clicking submit, I encountered the following error: enter image desc ...

The $route object in vue-router appears to be empty when used with single file components

I am facing an issue while using single file components with vue-router and vue-2.0. The problem I can't seem to resolve is that the this.$route object, when called from a component, always returns empty values. For example: https://i.sstatic.net/ws ...

Error Encountered When Loading the 'Oracle.DataAccess' File or Assembly

I encountered an error when running the application in asp.net. The error message reads: "Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. The located assembly's manifest definition does not match the assembly ...

Is it best practice to display a DIV once it is no longer visible after scrolling down?

Upon opening my website, an information box (div) appears on the screen. However, when the user scrolls down and the box is no longer visible, I want it to always appear in the top right corner while scrolling. I have figured out how to keep it visible at ...

Guidelines for submitting and displaying content on a single page with jQuery and MySQL

Objective: To develop a Q&A Script using PHP, JavaScript, and jQuery that allows users to post questions and provide answers. Upon submitting a new answer, it should be stored in the database and automatically displayed in the answers section. Challenge: ...

Tips on deleting information from an object by utilizing Chrome storage mechanisms

In the chrome storage, I have an object structured as follows: { "planA": { 123: {key: 'some key'} 124: {key: 'some other key'} }, "planB": { 223: {key: 'some key'} 234: { ...

Issues with the execution of Jquery function

Hey guys, take a look at my code: //Additional Jquery codes // display_popup_crop : revealing the crop popup function display_popup_crop(link) { alert("show_popup_crop function is triggered!"); // changing the photo source $('#cropbox&a ...

Refreshing the "OnMounted" Vue3 component

Recently, I encountered a situation where I have a Vue3 Dynamic Component enclosed within a <keep-alive> tag. This component facilitates navigation within a PrimeVue <Dialog> and is populated by an object: const component = [ { id: 0, ...

Difficulty arises when applying hover effects to animations using callbacks

Currently facing an issue with the hover event in jQuery. There are two side-by-side containers with hover events on both. When hovering, a div containing additional information slides up into view and slides back down when the hover ends. The concept is ...

Can you tell me the alternative for window.location.href when using Vue router?

After performing an action in my Vue app, I am looking to redirect to a different page within my single-page application (SPA). However, when I use the code window.location.href = "/subpage", the page refreshes and I end up losing cached data tha ...

Facing troubles with the file format while trying to upload a CSV file to dropbox.com

My goal is to develop a Chrome extension that allows users to upload files directly to Dropbox.com. While most aspects of the functionality are working smoothly, I am encountering some challenges with the CSV format. The code snippet below demonstrates how ...

Having trouble with dynamic path generation for router-link in Vuejs when using a v-for loop?

//main.js import Vue from "vue"; import App from "./App.vue"; import VueRouter from "vue-router"; import HelloWorld from "./components/HelloWorld.vue"; Vue.use(VueRouter); const router = new VueRouter({ routes: [{ path: "/", component: HelloWorld }] } ...

Utilize React-markdown to interpret subscript text in markdown format

I tried to create subscript text in an .md file using the following syntax: x_i x~i~ Unfortunately, react-markdown did not interpret this as subscript. After some research, I discovered the package remark-sub-super and implemented it with the plugin ...

Any suggestions on how to address vulnerabilities in npm when upgrading the main dependency version is not an option?

I recently ran the npm audit --production command and found a high-risk vulnerability related to the snowflake-sdk dependency. After checking the snowflake github page, I noticed that the package.json file includes "requestretry": "^6.0.0&qu ...

Cannot upload pictures using pixi.js PIXI.Texture.from()

I'm currently developing a web game using React with PixiJS. I am trying to incorporate images from my local directory into Pixi.js. For reference, I found this helpful website: Here are the snippets of code that I have used: import * as PIXI from & ...

Determining the Character Count in a Textbox Using Asp.net

I'm looking for a way to dynamically display the current number of characters written in a textbox in an asp label while the user is typing. I've searched online for solutions but haven't been able to find one that works. I attempted to imp ...

Does the concept of an Asynchronous SQLDataTable returned from ExecuteReaderAsync seem reasonable?

In my ASP.NET 4.5 application, I have set <httpRuntime targetFramework="4.5" />. One of my data helper library methods currently returns a SQLDatatable using SQLDataAdapter.Fill(). Now, I want to create a new method that returns Task(of SQLDatatabl ...

What is the best way to superimpose an image onto a canvas?

I am working on a cool project where I have created a canvas that displays matrix binary code raining down. However, I would like to enhance it by adding an image overlay on top of the canvas. Here is my current setup: <div class="rain"> ...