【发布时间】:2016-04-13 02:09:11
【问题描述】:
我需要有关如何在页面上显示谷歌地图的帮助。我使用了开发人员谷歌控制台的示例代码,我从他们那里获得了一个密钥,但是当我加载 html 文件时没有显示任何内容。
javascript代码如下
// Reference to the Firebase database.
var firebase = new Firebase("http://firebaseio.com");
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 3
});
// Add marker on user click
map.addListener('click', function(e) {
firebase.push({lat: e.latLng.lat(), lng: e.latLng.lng()});
});
// Create a heatmap.
var heatmap = new google.maps.visualization.HeatmapLayer({
data: [],
map: map,
radius: 8
});
firebase.on("child_added", function(snapshot, prevChildKey) {
// Get latitude and longitude from Firebase.
var newPosition = snapshot.val();
// Create a google.maps.LatLng object for the position of the marker.
// A LatLng object literal (as above) could be used, but the heatmap
// in the next step requires a google.maps.LatLng object.
var latLng = new google.maps.LatLng(newPosition.lat, newPosition.lng);
heatmap.getData().push(latLng);
});
}
html代码如下
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body { height: 100%; margin: 0; padding: 0; }
#map { height: 100%; }
</style>
<script src="https://cdn.firebase.com/js/client/2.2.1/firebase.js"></script>
</head>
<body>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC7Zr0DZ906x5OH2ODTJZ-O2CxO00khPnI&libraries=visualization&callback=initMap">
</script>
<script src="map.js"> </script>
</body>
</html>
【问题讨论】:
-
我刚刚用记事本保存了 html 和 javascript 代码,我在本地而不是远程运行。
-
谷歌地图在本地服务器上不起作用,如果你连接到互联网然后工作正常..
标签: javascript java php android jakarta-ee