question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add HTML Google Maps

See original GitHub issue

I am trying to use HTML Google Maps inside the ‘HTML’ option. Below is my code.

const express = require(‘express’) const app = express(); const nodeHtmlToImage = require(‘node-html-to-image’);

app.get(‘/’, async function (req, res) { const images = await nodeHtmlToImage({ html: <html> <head> <link href="https://fonts.googleapis.com/css?family=Poppins:400,700,900" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB4DWWpIHA-o8nFtCCh-IAYwgaUbdUW280"></script> <style> body { font-family: "Poppins"; font-weight: 100; } img { max-width: 100%; } </style> </head> <body> Hello world {{name}}! <div id='map-canvas'></div> <script type="text/javascript"> $(document).ready(function () { var mapOptions = { zoom: 16, // initialize zoom level - the max value is 21 streetViewControl: false, // hide the yellow Street View pegman scaleControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(32.920, -96.773), }; var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); }); </script> </body> </html>, content: [{ name: ‘Praveen’ }] }); res.writeHead(200, { ‘Content-Type’: ‘image/png’ }); res.end(images[0], ‘binary’); }); app.listen(3000)

I’m able to get the result but the map is not showing in the background.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
frinyvonnickcommented, Feb 8, 2022

Since it fixed your issue, I close it 👍

1reaction
frinyvonnickcommented, Feb 8, 2022

@colonelEnigma I managed to make it work. Here is the js code:

const express = require("express");
const app = express();
const nodeHtmlToImage = require("node-html-to-image");

app.get("/", async function (req, res) {
  const images = await nodeHtmlToImage({
    html: `<!DOCTYPE html>
<html>
<head>
<link href="styles.css" media="screen" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB4DWWpIHA-o8nFtCCh-IAYwgaUbdUW280"></script>
</head>
<body>
<div id='container'>
<h1>OMG, this is ON TOP OF THE MAP!</h1>
</div>

<div id='map-canvas' style="height:300px"></div>

<script type="text/javascript">
	$(document).ready(function () {
   var mapOptions = {
      zoom: 16, // initialize zoom level - the max value is 21
      streetViewControl: false, // hide the yellow Street View pegman
      scaleControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: new google.maps.LatLng(32.920, -96.773),
   };
  new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
});
</script>
</body>
</html>`,
    content: [{ name: "Praveen" }],
  });
  res.writeHead(200, { "Content-Type": "image/png" });
  res.end(images[0], "binary");
});
app.listen(3000);

Since I don’t have the style.css file from you example I had to put a height on the div #map-canvas (see <div id='map-canvas' style="height:300px"></div>) otherwise the map was not displayed because the div’s height was 0.

Here is the result I get running this code:

image

It looks good to me. Can you confirm?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding a Google Map with a Marker to Your Website
This tutorial shows you how to add a simple Google map with a marker to a web page. It suits people with beginner...
Read more >
Google Maps Tutorial - W3Schools
An API is a set of methods and tools that can be used for building software applications. Google Maps in HTML. This example...
Read more >
How to add Google map inside html page without using API key
How to add Google map inside html page without using API key ? · Go to the google maps and search your desired...
Read more >
Share a map or directions with others - Google Support
Open Google Maps. · Go to the directions, map, or Street View image you'd like to embed. · In the top left, click...
Read more >
How to Create a Google Map in HTML? - eduCBA
We will call the google map API on our web page using <script> tag. We can set the marker after adding the google...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found