Click on a custom marker on Google Maps to reveal a pop-up info window with more details

I have successfully created a personalized Google Map using Custom Markers. Now, I am looking to incorporate Info Windows for each marker.

The code for the Custom Markers can be found at: https://developers.google.com/maps/documentation/javascript/custom-markers

I attempted to add Info windows following this example: https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

While researching, I came across a somewhat related question here:

Here is a snippet of my code:

  html,
  body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
  
  #map {
    height: 100%;
  }
<div id="map"></div>

<script>
  var map;

  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 17,
      center: new google.maps.LatLng(40.712696, -74.005019),
      mapTypeId: 'roadmap'
    });

    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
    var icons = {
      parking: {
        icon: iconBase + 'parking_lot_maps.png'
      },
      library: {
        icon: iconBase + 'library_maps.png'
      },
      info: {
        icon: iconBase + 'info-i_maps.png'
      }
    };

    var features = [{
      position: new google.maps.LatLng(40.712696, -74.005019),
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.712753, -74.006081),
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.713664, -74.007819),
      type: 'library'
    }];

    // Create markers.
    features.forEach(function(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        icon: icons[feature.type].icon,
        map: map
      });
    });
  }

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap">


</script>

Also, here is the JSFiddle link for reference: https://jsfiddle.net/vishnuprasadps/7g33j2kz/

Answer №1

What content would you like to display in your infoWindow?

Here is a solution that may work for you:

<div id="map"></div>

<script>
  var map;

  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 17,
      center: new google.maps.LatLng(40.712696, -74.005019),
      mapTypeId: 'roadmap'
    });

    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
    var icons = {
      parking: {
        icon: iconBase + 'parking_lot_maps.png'
      },
      library: {
        icon: iconBase + 'library_maps.png'
      },
      info: {
        icon: iconBase + 'info-i_maps.png'
      }
    };

    var features = [{
      position: new google.maps.LatLng(40.712696, -74.005019),
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.712753, -74.006081),
      type: 'parking'
    }, {
      position: new google.maps.LatLng(40.713664, -74.007819),
      type: 'library'
    }];

    var infowindow = new google.maps.InfoWindow({
      content: "test"
    });

    // Create markers.
    features.forEach(function(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        icon: icons[feature.type].icon,
        map: map
      });
      marker.addListener('click', function() {
        infowindow.open(map, marker);
      });
    });





  }

</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap">


</script>

https://jsfiddle.net/uqLxnyca/

Wishing you a wonderful day.

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

Deactivate click events in the container div

Here is the html code snippet that I am working with: <div class="parent" ng-click="ParentClick()"> . . . <div class="child" ng-click="ChildClick()"> Some Text </div> </div> When clicking on Som ...

Alert: CSS 4.0 Warning Validation - The value "rgb(0, 0, 0, 0.4)" is not valid for the "border" property

Wrapping up my asp core 3 project, I've been addressing various warnings that have popped up. However, one particular warning has me stumped. The CSS property in question is functioning perfectly, adding a touch of transparency to the black border. Wh ...

What is the best way to retrieve a JSON value when there are five variables with identical names?

I am in the process of creating a NSFW filter and I am looking for the results of the analysis. Specifically, I need to determine if the probabilities of "Pºrn" and "Hºntai" are greater than 0.7. However, I am encountering an issue with accessing the ind ...

Javascript - Conceal a Dynamic Div Depending on its Text

I have a unique table that generates dynamic Divs with ID's that count as they are created in the following format: <div id="dgpCheckDiv10_0"> <input Delete </div> <div id="dgpCheckDiv10_1"> text2 </div> Some of t ...

Is it possible to integrate Laravel as the backend for an already existing HTML website template?

I recently designed an HTML template for a salon listing directory site, and while it looks great, I realize I also need a backend solution to manage listings, add advertisements, and more. Can I seamlessly integrate a Laravel-based admin panel into my c ...

The input box within the form is insufficiently sized

My task is to generate textareas with custom parameters like "cols" and "rows" and display them in forms. However, these parameters seem not to be functioning as expected. Whether I set cols=1000 or cols=1, there is no difference in the output. After revie ...

Prevent removal of h2 tag within a contenteditable segment

Can a section be made permanent within a contenteditable element to prevent user removal? I have an h2 tag inside a contentEditable div. I do not want the user to be able to edit the h2 tag, so I set contentEditable=false, but they can still select and de ...

Disabling a text area or mat-form-field within the same HTML file in Angular 9

Currently, I am working with Angular 9 and angular Material Reactive Forms. My goal is to disable certain fields without relying on the disabled status within formControl in the TypeScript file. Instead, I want to achieve this solely through HTML. Here is ...

Decrease the border-radius shorthand mixin, deactivate the variable

I have been working on creating a mixin for border-radius that will only apply when the values, determined by a variable, are greater than or equal to 0. I have set the default value in a variable as 3px, so if I input -1 or 'no', the border-radi ...

Adjusting the dimensions of a Twitter widget to fit the screen size

My website is designed to resize based on screen size, but I encountered an issue when adding a Twitter widget. Despite setting the attribute width:'auto', the widget did not resize properly. Below is the code for the widget: <script charset= ...

I'm looking to fetch my post request with JavaScript - does anyone know how to

In my current setup, I have two main projects. The first project involves using Node.JS for handling data transfer tasks. jsonobj = JSON.stringify(generateMockData) xhrToSoftware.send(jsonobj); xhrToAPI.open("POST", "http://127.0.0.1:8000/pa ...

Retrieve the variable within the $.ajax function using the onSubmit event

After spending hours today trying to solve my problem, I found myself stuck. My form has a tag onsubmit="return checking();". The checking() function is responsible for input validation. function checking(){ var filename = $("#filename").val(); $. ...

Encountering a hitch while attempting to integrate a framework in Angular JS 1

Having experience with Angular JS 1 in my projects, I have always found it to work well. However, I recently encountered a project that uses Python and Django, with some pages incorporating Angular. The specific page I needed to work on did not have any An ...

Checking HTML and CSS validation when uploading files in Ruby on Rails

Currently, the project I am working on enables users to upload HTML templates along with CSS, which are then processed by the application for a variety of tasks. While the uploading process is functioning properly, I am in search of a method to validate th ...

What is the best way to incorporate JavaScript code as React syntax?

For my project, I am using the OpenLayers API Map. I'm unsure of the correct way to incorporate JavaScript code in React or vice versa. Here is an answer on how to use markers in the map, but I am struggling to implement it in my current code since I ...

Is there a way for me to automatically update the contents of div x using Ajax whenever the contents of div y are changed with another Ajax call?

I've been struggling with this issue for quite some time now and haven't been able to find a solution. The situation is as follows: I have two divs, where div3 needs to update its content whenever div2 changes. Div2 is updated using ajax, so whe ...

error encountered while attempting to import a function from an npm script due to module

//test.spec.js import { myCustomGlob } from './scripts/index' describe.only('generate file', () => { console.log(myCustomGlob) //MODULE NOT FOUND ERROR }) //script const fs = require('fs') const myCustomGlob = () => ...

Implementing multiple filters for object arrays in Angular 8

On my current project, I am interested in implementing multiple filters. The filters I want to use include price range, type, and uploaded date. For uploaded date, I have a checkbox with options for Today, Last 2 days, Last 7 days, and Any. When it come ...

Toggle button in React following a list iteration

Upon receiving data from an API call to Google Books, I want to hide the description paragraphs and implement a toggle button using the "hidden" CSS class from Tailwind CSS. Currently, I am just logging the elements on the "view description" button and uns ...

What is the process for obtaining the source code for children in React?

Can React extract the source code from children props? Imagine having a component with the following structure... <SomeComp> <Heading>I want to access this code inside the Heading tag</Heading> </SomeComp> How can I retrieve t ...