我想有一个弹出窗口,其中包含存储在geojson中的每个点的属性。我可以让地图和标记进行渲染,但是当我点击该点时,没有包含属性的弹出框。
我确信这很简单,但我尝试过this,this和this,但仍然无法让它发挥作用......
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 100%;
height: 600px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script src="POINTS.js"></script>
<script>
function initialize() {
features = []
var mapDiv = document.getElementById('map');
map = new google.maps.Map(mapDiv, {
center: {lat: 44.540, lng: -78.546},
zoom: 2
});
// When the user clicks, open an infowindow
var infowindow = new google.maps.InfoWindow();
map.data.addListener('click', function(event) {
var myHTML = event.feature.getProperty("id");
infowindow.setContent("<div style='width:150px;'>"+myHTML+"</div>");
// position the infowindow on the marker
var anchor = new google.maps.MVCObject();
anchor.set("position", event.latLng);
infoWindow.open(map, anchor);
});
// load data
map.data.addGeoJson(data);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=visualization&callback=initialize">
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
这是我的POINT.js
var data = {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "X": 144.961836, "Y": -36.749381 }, "geometry": { "type": "Point", "coordinates": [ 144.961836873913285, -36.749381167692619 ] } }
]
}