【发布时间】:2017-08-28 07:24:27
【问题描述】:
我正在渲染 maps.ejs 文件:place_id、lat、lng。
如果我在 initMap 函数本身的 maps.ejs 文件中声明“place_id”,则代码运行良好,没有任何错误。 但是当我按照代码所示渲染 place_id 时,它会在 chrome 的控制台中出现错误:
(index):34 Uncaught ReferenceError: ChIJN1t_tDeuEmsRUsoyG83frY4 未定义 在 initMap ((index):34)
我正在使用 cloud9 并将 API_KEY 作为环境变量导出。
//app.js
var express = require("express");
var app = express();
app.get("/", function(req, res) {
res.render("maps.ejs", {
place_id: "ChIJN1t_tDeuEmsRUsoyG83frY4",
lat: -33.8666199,
lng: 151.1958527
});
});
app.listen(process.env.PORT, process.env.IP, function() {
console.log("Server is ON");
}); // Server start
//maps.ejs
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<title>Google Maps API</title>
<body>
<h3>My Google Maps Demo</h3>
<script>
function initMap() {
var uluru = {
lat: <%=lat%>,
lng: <%=lng%>
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: uluru
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: <%=place_id%>
}, function(place, status) {
console.log(place);
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(
'<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '<br>' +
"Rating : " + place.rating + '</div>');
infowindow.open(map, this);
});
}
});
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=<%=process.env.API_KEY%>&libraries=places&callback=initMap">
</script>
<div id="map"></div>
</body>
</html>
【问题讨论】:
标签: javascript node.js google-maps google-maps-api-3 ejs