javascript - 在 Node.js/Express 上加载 Google Maps API 简单地图
问题描述
我正在尝试创建一个在服务器端加载 Google Maps API 的 Web 应用程序,因为我想在其地图上加载标记。
以下是相关 Web 应用程序的项目结构: 项目文件结构
如果我在浏览器上加载 HTML 文件,地图就会显示在屏幕上。
但是,如果我在 node.js 上加载运行 app.js,我会得到一个几乎空白的屏幕。 几乎黑屏
甚至我自己也不确定是否要将 html 呈现为静态文件,因为它涉及到如此多的动态元素。
这是我的 css html 和 js 文件的样子。注意:出于安全目的,API 密钥不会显示。
应用程序.js
const express = require('express');
const port = 3000;
const path = require('path');
const app = express();
app.get('/', function(req,res) {
res.sendFile(__dirname + '/index.html');
//__dirname : It will resolve to your project folder.
});
//add the router
app.listen(port, () => console.log(`listening on port ${port}!`));
索引.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>CTA Bus Live</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script src="./index.js"></script>
</head>
<body>
<div id="map"></div>
<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap">
</script>
<script src="http://ctabustracker.com/bustime/api/v2/gettime?vid=4367&key=wGSShQesdeEhNFS92HF2BKDyS&format=json"></script> --
</body>
</html>
index.js
let map, infoWindows;
function initMap() {
const styledMapType = new google.maps.StyledMapType(
[{
featureType: "poi",
elementType: "labels",
stylers: [{
visibility: "off"
}],
}, ], {
name: "Transit"
}
);
map = new google.maps.Map(document.getElementById("map"), {
center: {
lat: 41.8774,
lng: -87.6319
},
zoom: 15,
mapTypeControlOptions: {
mapTypeIds: ["roadmap", "satellite", "hybrid", "terrain", "styled_map"],
},
});
var myLatlng = new google.maps.LatLng(41.831677, -87.625826);
var marker = new google.maps.Marker({
position: myLatlng,
title: "CTA Green Line",
icon: {url: "https://www.iconsdb.com/icons/preview/color/009B3A/circle-xxl.png",
scaledSize: new google.maps.Size(15, 15)},
optimized: false
});
// To add the marker to the map, call setMap();
marker.setMap(map);
map.mapTypes.set("styled_map", styledMapType);
map.setMapTypeId("styled_map");
const transitLayer = new google.maps.TransitLayer();
if (map.getMapTypeId() === "styled_map") {
transitLayer.setMap(map);
}
map.addListener("maptypeid_changed", () => {
if (map.getMapTypeId() !== "styled_map") {
transitLayer.setMap(null);
}
if (map.getMapTypeId() === "styled_map") {
transitLayer.setMap(map);
}
});
}
样式.css
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
这就是我希望我的页面在节点上运行时的样子: 目标
解决方案
推荐阅读
- vulkan - 帧缓冲区在 Vulkan 中究竟是什么意思
- reactjs - 使用来自 GraphQL 的数据获取 Material-UI 表的动态行数
- java - 在 pom.xml 中添加 google-cloud-pubsub 版本 1.107.0 时下载的编译依赖项较低
- java - 如何在 Android 10 中访问剪贴板数据?
- ffmpeg - 如何在使用 ffmpeg 计算 vmaf 分数时同步帧
- time-series - 在对时间序列的子集进行聚类后,如何将剩余的时间序列与已创建的聚类相关联?
- laravel - 如何从刀片中的循环中获取记录的当前月份
- javascript - Javascript:如何从构造函数调用方法?我的变量返回未定义
- git - RPC 失败;HTTP 400 curl 22 The requested URL returned error: 400 Bad Request
- c# - 如何使 DualPivot Quicksort 变得更好/更快