我没有使用谷歌地图的经验。我在地图中加载了一个geojson,我在click事件上有一个监听器来放置一个标记。问题是,当我点击区域外但不在内部时,它会起作用。
var map;
function initMap() {
var mapCanvas = document.getElementById("map");
var mapOptions = {
center: {lat: 41.8708, lng: -87.6505},
zoom: 14
}
var map = new google.maps.Map(mapCanvas, mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(map, event.latLng);
});
// map.data.loadGeoJson('https://gist.githubusercontent.com/cgansen/6458644/raw/5a9ec31defab0073853831b7fc7de0f87eb66a2d/wards-from-city-unsimplified.geojson');
map.data.loadGeoJson('https://data.cityofchicago.org/api/geospatial/cauq-8yn6?method=export&format=GeoJSON');
var myLatLng = {lat: 41.8708, lng: -87.6505};
//Illinois University Marker
var image = 'js/i1.png';
var Umarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Department of Computer Science – University of Illinois!',
icon: image
});
}
function placeMarker(map, location) {
var marker = new google.maps.Marker({
position: location,
map: map
});

答案 0 :(得分:0)
所以,我改变了你的代码 - 但你正在寻找的秘诀如下:
// Add the click event to the new polygon layer as well as the map itself.
map.data.addListener('click', function(event) {
placeMarker(map, event.latLng)
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(map, event.latLng);
});
JSFiddle:http://jsfiddle.net/cguju62a/
祝你好运!