Having trouble loading the Google map after taking a screenshot using html2canvas

I recently attempted to use html2canvas to capture a Google Map image, but unfortunately, the resulting image only displayed the map's direction and not the actual map. Can anyone provide assistance with this issue? Below is the code snippet along with a reference image.

function getScreenShot() {

      html2canvas(document.querySelector("#map")).then(canvas => {

      var img = canvas.toDataURL();
      window.open(img);
      height=height,
      width=width

  });
 }

View original image before capture.

View captured image for comparison.

Answer №1

Have you considered utilizing the staticmap feature of Google Maps? This allows you to easily generate an image of the map. Check out the function below for more information.

 function getScreenShot() {
    //google static map url
    var staticM_URL = "https://maps.googleapis.com/maps/api/staticmap";

    //Set the Google Map Center.
    staticM_URL += "?center=" + mapOptions.center.lat() + "," + mapOptions.center.lng();

    staticM_URL  += "&size=520x650";  //Set the Google Map Size.

    staticM_URL  += "&zoom=" + mapOptions.zoom;  //Set the Google Map Zoom.

    staticM_URL  += "&maptype=" + mapOptions.mapTypeId;//Set the Google Map Type.

    //Loop and add Markers.
    for (var i = 0; i < markers.length; i++) {
        staticM_URL  += "&markers=color:red|" + markers[i].lat + "," + markers[i].lng;
    }

   window.open(staticM_URL);
}

If you need further assistance, you can refer to these URLs:

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

Caution notification using Javascript

Currently, I have integrated simple JavaScript code into my PHP code to show an alert message: <script type="text/javascript"> window.alert("Can not Send Messages ") </script> However, when the alert pops up, the original page vanishes. A ...

What is the best way to display ajax response.Text as HTML?

My current approach involves using an Ajax request query to retrieve variables containing HTML code from a php file. To display the retrieved content on the page, I am employing the following method: document.getElementById('my_div').innerHTML = ...

Storing items in an array within the local storage

I have been experimenting with different ways to add objects to an array in localstorage without overwriting it, but I haven't found a successful solution yet. My current code stores objects in an array, but it keeps overwriting itself. Can someone p ...

having difficulty sorting items by tag groups in mongodb using $and and $in operators

I'm currently trying to execute this find() function: Item.find({'tags.id': { $and: [ { $in: [ '530f728706fa296e0a00000a', '5351d9df3412a38110000013' ] }, { $in: [ ...

Incorrect Alignment of Centered Boxes with CSS

I'm having trouble creating centered boxes with CSS. Currently, I have the following code: http://jsfiddle.net/LW7mN/2/ .trex-clear-all{ display: inline-block; width: 99%; height: 17px; font-size: 12px !important; text-align: -w ...

Step-by-step guide on how to superimpose a Glyphicon on

I'm struggling to figure out how to overlay a Glyphicon (.glyphicon-zoom-in) on top of a Bootstrap .img-thumbnail. The specific CSS rule I've been using is content:"\e015"; ...

Using a variable as a selector with jQuery's .remove method

My script involves a dynamic variable packet var packet = "" Each time the user clicks a button, the letter 'a' is added to the variable packet = packet + "a" This process also generates three div elements with unique ids based on the variab ...

Leveraging JavaScript to enable automatic identification of buttons on a webpage

My website features specific buttons placed within articles and also in the head/body/footer sections. I have a plan to make it so that when a user clicks on a button, JavaScript code will determine if the button is inside an article or outside of it, then ...

Tips for creating a breakpoint in Sass specifically for a 414px iPhone screen size

For my latest project, I am utilizing sass and looking to incorporate a sass breakpoint or media query. My goal is to have the code execute only if the screen size is 414px or less. Below is my existing code: @include media-breakpoint-down(sm) { .sub-men ...

Changing the class name in HTML depending on the size of the screen

I'm attempting to dynamically change the class tag's name when a user visits the page based on the screen size. Below is the code snippet I've used: <!DOCTYPE html> <html> <body onload="changeClass()"> <section class=" ...

The foundation grid system is not being implemented

How come my div is not affected by the foundation grid system: render: function(){ var {todos,showCompleted,searchText} = this.state; var filteredTodos = TodoAPI.filterTodos(todos,showCompleted,searchText); return ( <div> ...

Make a request using jQuery AJAX to retrieve data from a specific column

I'm trying to extract column data from my database using a jQuery AJAX request, but I keep running into issues. Can someone point out what I might be doing wrong? Basically, when I click a button, I want to retrieve an array of values. javascript : ...

Customize the Underline Color in MaterialUI Select Component

I'm experimenting with MaterialUI and trying to figure out how to customize the underline and text color of the Select component when an item is selected. Currently, the underline and label appear blue after selection with a gray background. Is there ...

Ways to verify if a value is null and replace it with placeholder text such as "NA"

I am facing an issue with an input field that is pulling data from the database. When the database is empty, the input field displays as disabled, which is meant to disable the field when it is not editable. Here is an example in my HTML: <label for=&q ...

What could be causing the padding of my anchor element to extend outside the boundaries of its parent list

In the provided images, it is evident that the <li> element is failing to expand and is disregarding the padding of the parent element.</p> <p><a href="https://i.stack.imgur.com/4ZH65.png" rel="nofollow noreferrer"></a></p& ...

Creating interactive button elements in daily views with FullCalendar and Vue3

For the screen design, I have opted to go with Vuejs and specifically using the FullCalendar library https://i.sstatic.net/pn4ic.png The FullCalendar component has been successfully added. I am facing difficulties in rendering a button with DaycellConte ...

Executing JavaScript functions within static files in Django

I can't figure out why I'm unable to invoke a javascript function when clicking my Bootstrap button in a Django project. In my directory, I have set up the static folder as "main/static/main/js/script.js". While I can successfully load the stati ...

The error message "Next.js 14 does not recognize res.status as a function"

I'm a newcomer to Next.js and I've been wrestling with an issue all day. Here's my API for checking in MongoDB if a user exists: import {connect} from '../../../../lib/mongodb'; import User from '../../../models/userModel&ap ...

Strategies for improving checkbox selection logic and reducing the number of loops in React

In my current React project, I have implemented multiple lists of items with checkboxes, inspired by Shopee's shopping cart page. While I've successfully added the checkbox functionality using React state and event handling for individual checkbo ...

Error loading content for webpack://SwaggerUIBundle/src/core/system.js in Swagger UI

When trying to utilize Swagger UI for documenting a Laravel project using the URL http://localhost:8014/api/documentation, I came across an error displayed in the browser console: Error: Unable to destructure property 'type' of 'u' as i ...