简体   繁体   English

使用 Express 和 Node.js 的谷歌地图 API

[英]Google Maps API using Express and Node.js

I'm trying to create a basic web app using the google maps API with express/node.我正在尝试使用带有 express/node 的 google maps API 创建一个基本的网络应用程序。 Currently my three files are as follows:目前我的三个文件如下:

server.js服务器.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express()

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')

// Initialize and add the map
function initMap() {
    // The location of Uluru
    var uluru = {
      lat: -25.344,
      lng: 131.036
    };
    // The map, centered at Uluru
    var map = new google.maps.Map(
      document.getElementById('map'), {
        zoom: 4,
        center: uluru
      });
    // The marker, positioned at Uluru
    var marker = new google.maps.Marker({
      position: uluru,
      map: map
    });
  }

app.get('/', function (req, res) {
  res.render('index');
})

app.post('/', function (req, res) {
    res.render('index');
  })

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

style.css样式.css

#map {
    height: 400px; 
    width: 100%; 
   }

index.html索引.html

<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=API&callback=initMap">
</script>

When i run the app the error returned is "initMap is not a function".当我运行该应用程序时,返回的错误是“initMap 不是一个函数”。 I have been reading through examples online but everything i have tried has not worked.我一直在阅读在线示例,但我尝试过的一切都没有奏效。 I would like to keep as little javascript away from my HTML file as possible.我希望尽可能少地使用我的 HTML 文件中的 javascript。

There is nothing wrong with your code你的代码没有问题

It is working fine, check below:它工作正常,请检查以下内容:

Solution: apply initMap() function to client side javascript file, not in server.js解决方案:将initMap()函数应用于客户端 javascript 文件,而不是在server.js

 function initMap() { // The location of Uluru var uluru = { lat: -25.344, lng: 131.036 }; // The map, centered at Uluru var map = new google.maps.Map( document.getElementById('map'), { zoom: 4, center: uluru }); // The marker, positioned at Uluru var marker = new google.maps.Marker({ position: uluru, map: map }); }
 #map { height: 400px; width: 100%; }
 <script async defer src="https://maps.googleapis.com/maps/api/js?key=_____&callback=initMap"> </script> <h3>My Google Maps Demo</h3> <div id="map"></div>

Note: NEVER SHARE YOUR API-Key to anyone!注意:切勿与任何人分享您的 API 密钥!

IMPORANT: Do not show your API-key.重要提示:不要显示您的 API 密钥。 People can take it and use it for their own, and you can get a huge invoice.人们可以把它据为己有,你可以得到一张巨额发票。

What you are doing currently is creating markers in Node.js, which is backend.您目前正在做的是在后端的 Node.js 中创建标记。 The library that you use (maps.googleapis.com) is used on the frontend.您使用的库 (maps.googleapis.com) 用于前端。 So what you should do is a new javascript file that you have referenced in the HTML-file.所以你应该做的是你在 HTML 文件中引用的一个新的 javascript 文件。

Example:例子:

index.html索引.html

<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=key&callback=initMap">
</script>
<script>
function initMap() {
    // The location of Uluru
    var uluru = {
      lat: -25.344,
      lng: 131.036
    };
    // The map, centered at Uluru
    var map = new google.maps.Map(
      document.getElementById('map'), {
        zoom: 4,
        center: uluru
      });
    // The marker, positioned at Uluru
    var marker = new google.maps.Marker({
      position: uluru,
      map: map
    });
  }
</script>

If you dont want javascript inline, create a new file in the same directory as index.html, and reference it with <script src="script.js"></script>如果您不想内联 javascript,请在与 index.html 相同的目录中创建一个新文件,并使用<script src="script.js"></script>引用它

I hope this will help you我希望这能帮到您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM