Google 地图下拉选择器显示信息窗口
Google Maps Dropdown Selector to show InfoWindow
我创建了一个 Google 带有聚类标记的地图和一个显示每个位置的下拉菜单。
这个想法是,当您 select 从下拉菜单中选择一个位置时,会显示信息窗口。
一切正常 - 除了最后一点。
当某个位置被 select 编辑时,我无法让 Select 下拉菜单显示信息窗口。
代码如下:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[API-KEY]&callback=initMap&language=en"></script>
<script src="../markerclustererplus@4.0.1.min.js"></script>
<script type="text/javascript">
var gmarkers = [];
var side_bar_html = "";
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 1, lng: 1},
zoom: 3,
minZoom: 1,
maxZoom: 16,
mapTypeControl: false,
streetViewControl: false,
scrollwheel: false
});
var infoWin = new google.maps.InfoWindow();
var markers = locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: '../images/location-marker.png'
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
return marker;
});
var markerCluster = new MarkerClusterer(map, [], {
imagePath: '../images/m'
});
markerCluster.setStyles(markerCluster.getStyles().map(function (style) {
style.textColor = '#fff';
return style;
}));
markerCluster.addMarkers(markers)
}
var locations = [
{
title: "Marker 1",
lat: 0,
lng: 0,
info: "Info Window 1"
},
{
title: "Marker 2",
lat: 0,
lng: 1,
info: "Info Window 2"
},
{
title: "Marker 3",
lat: 0,
lng: 2,
info: "Info Window 3"
},
{
title: "Marker 4",
lat: 1,
lng: 0,
info: "Info Window 4"
},
{
title: "Marker 5",
lat: 1,
lng: 1,
info: "Info Window 5"
},
{
title: "Marker 6",
lat: 1,
lng: 2,
info: "Info Window 6"
},
];
for (var i = 0; i < locations.length; i++) {
marker = addMarker(i);
}
document.getElementById("side_bar").innerHTML = "<select onchange='myclick(this.value);' name=\"country\" class=\"wpcf7-form-control wpcf7-select form-control\" aria-invalid=\"false\"><option value=\"Please choose your country...\">Please choose your country...</option>"+side_bar_html+"</select>";
function addMarker(i) {
var draftMarker = locations[i];
var myLatLng = new google.maps.LatLng(draftMarker.lat, draftMarker.lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: draftMarker.title,
icon: '../images/location-marker.png'
});
google.maps.event.addListener(location, 'click', function () {
info.setContent(draftMarker.info);
info.open(map, location);
});
gmarkers.push(location);
side_bar_html += '<option value=' + (gmarkers.length-1) + '>' + draftMarker.title + '<\/option>';
return marker;
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}
google.maps.event.addDomListener(window, "load", initMap);
</script>
在 Google Chrome
的控制台中查看时出现以下错误
Uncaught TypeError: gmarkers[i].getPosition is not a function
我尝试更改最后一位
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}
到
function myclick(i) {
var draftMarker = locations[I];
map.setCenter(new google.maps.LatLng(draftMarker.lat, draftMarker.lng));
google.maps.event.trigger(gmarkers[i], "click");
}
但是我得到了错误
Uncaught TypeError: map.setCenter is not a function
您的代码中存在多个问题。首先,确保始终考虑范围。您需要将一些变量设置为全局变量,以便在您未明确设置它们的函数中检索它们的值:
var infoWin;
var map;
这就是您收到“gmarkers[i].getPosition 不是函数”错误的原因。
然后,您将在 initMap
和 addMarker
中添加标记及其对应的点击事件监听器,但是当您点击它们时,您只会触发第一个事件监听器,即信息 windows 无法打开的另一个原因。您只需要在一个地方添加所有代码一次,例如在 addMarker
。此外,当您应该推送实际标记实例时,您正在使用 gmarkers.push(location)
,即 gmarkers.push(marker)
.
我已将您的 JS 代码修复如下:
var gmarkers = [];
var side_bar_html = "";
var infoWin;
var map;
var locations = [
{
title: "Marker 1",
lat: 0,
lng: 0,
info: "Info Window 1"
},
{
title: "Marker 2",
lat: 0,
lng: 1,
info: "Info Window 2"
},
{
title: "Marker 3",
lat: 0,
lng: 2,
info: "Info Window 3"
},
{
title: "Marker 4",
lat: 1,
lng: 0,
info: "Info Window 4"
},
{
title: "Marker 5",
lat: 1,
lng: 1,
info: "Info Window 5"
},
{
title: "Marker 6",
lat: 1,
lng: 2,
info: "Info Window 6"
}
];
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: { lat: 10, lng: 10 },
zoom: 3,
minZoom: 1,
maxZoom: 16,
mapTypeControl: false,
streetViewControl: false,
scrollwheel: false
});
infoWin = new google.maps.InfoWindow();
var markers = locations.map(function (location, i) {
return addMarker(i);
});
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML =
'<select onchange=\'myclick(this.value);\' name="country" class="wpcf7-form-control wpcf7-select form-control" aria-invalid="false"><option value="Please choose your country...">Please choose your country...</option>' +
side_bar_html +
"</select>";
var markerCluster = new MarkerClusterer(map, [], {
imagePath: "https://www.takagi-j.com/test/marker-clusterer-example/images/m"
});
markerCluster.setStyles(
markerCluster.getStyles().map(function (style) {
style.textColor = "#fff";
return style;
})
);
markerCluster.addMarkers(markers);
}
function addMarker(i) {
infoWin = new google.maps.InfoWindow();
var draftMarker = locations[i];
var myLatLng = new google.maps.LatLng(draftMarker.lat, draftMarker.lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: draftMarker.title,
icon:
"https://www.takagi-j.com/test/marker-clusterer-example/images/location-marker.png"
});
google.maps.event.addListener(marker, "click", function (evt) {
infoWin.setContent(draftMarker.info);
infoWin.open(map, marker);
});
gmarkers.push(marker);
side_bar_html +=
"<option value=" +
(gmarkers.length - 1) +
">" +
draftMarker.title +
"</option>";
return marker;
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}
google.maps.event.addDomListener(window, "load", initMap);
我创建了一个 Google 带有聚类标记的地图和一个显示每个位置的下拉菜单。 这个想法是,当您 select 从下拉菜单中选择一个位置时,会显示信息窗口。
一切正常 - 除了最后一点。
当某个位置被 select 编辑时,我无法让 Select 下拉菜单显示信息窗口。
代码如下:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[API-KEY]&callback=initMap&language=en"></script>
<script src="../markerclustererplus@4.0.1.min.js"></script>
<script type="text/javascript">
var gmarkers = [];
var side_bar_html = "";
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 1, lng: 1},
zoom: 3,
minZoom: 1,
maxZoom: 16,
mapTypeControl: false,
streetViewControl: false,
scrollwheel: false
});
var infoWin = new google.maps.InfoWindow();
var markers = locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: '../images/location-marker.png'
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
return marker;
});
var markerCluster = new MarkerClusterer(map, [], {
imagePath: '../images/m'
});
markerCluster.setStyles(markerCluster.getStyles().map(function (style) {
style.textColor = '#fff';
return style;
}));
markerCluster.addMarkers(markers)
}
var locations = [
{
title: "Marker 1",
lat: 0,
lng: 0,
info: "Info Window 1"
},
{
title: "Marker 2",
lat: 0,
lng: 1,
info: "Info Window 2"
},
{
title: "Marker 3",
lat: 0,
lng: 2,
info: "Info Window 3"
},
{
title: "Marker 4",
lat: 1,
lng: 0,
info: "Info Window 4"
},
{
title: "Marker 5",
lat: 1,
lng: 1,
info: "Info Window 5"
},
{
title: "Marker 6",
lat: 1,
lng: 2,
info: "Info Window 6"
},
];
for (var i = 0; i < locations.length; i++) {
marker = addMarker(i);
}
document.getElementById("side_bar").innerHTML = "<select onchange='myclick(this.value);' name=\"country\" class=\"wpcf7-form-control wpcf7-select form-control\" aria-invalid=\"false\"><option value=\"Please choose your country...\">Please choose your country...</option>"+side_bar_html+"</select>";
function addMarker(i) {
var draftMarker = locations[i];
var myLatLng = new google.maps.LatLng(draftMarker.lat, draftMarker.lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: draftMarker.title,
icon: '../images/location-marker.png'
});
google.maps.event.addListener(location, 'click', function () {
info.setContent(draftMarker.info);
info.open(map, location);
});
gmarkers.push(location);
side_bar_html += '<option value=' + (gmarkers.length-1) + '>' + draftMarker.title + '<\/option>';
return marker;
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}
google.maps.event.addDomListener(window, "load", initMap);
</script>
在 Google Chrome
的控制台中查看时出现以下错误Uncaught TypeError: gmarkers[i].getPosition is not a function
我尝试更改最后一位
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}
到
function myclick(i) {
var draftMarker = locations[I];
map.setCenter(new google.maps.LatLng(draftMarker.lat, draftMarker.lng));
google.maps.event.trigger(gmarkers[i], "click");
}
但是我得到了错误
Uncaught TypeError: map.setCenter is not a function
您的代码中存在多个问题。首先,确保始终考虑范围。您需要将一些变量设置为全局变量,以便在您未明确设置它们的函数中检索它们的值:
var infoWin;
var map;
这就是您收到“gmarkers[i].getPosition 不是函数”错误的原因。
然后,您将在 initMap
和 addMarker
中添加标记及其对应的点击事件监听器,但是当您点击它们时,您只会触发第一个事件监听器,即信息 windows 无法打开的另一个原因。您只需要在一个地方添加所有代码一次,例如在 addMarker
。此外,当您应该推送实际标记实例时,您正在使用 gmarkers.push(location)
,即 gmarkers.push(marker)
.
我已将您的 JS 代码修复如下:
var gmarkers = [];
var side_bar_html = "";
var infoWin;
var map;
var locations = [
{
title: "Marker 1",
lat: 0,
lng: 0,
info: "Info Window 1"
},
{
title: "Marker 2",
lat: 0,
lng: 1,
info: "Info Window 2"
},
{
title: "Marker 3",
lat: 0,
lng: 2,
info: "Info Window 3"
},
{
title: "Marker 4",
lat: 1,
lng: 0,
info: "Info Window 4"
},
{
title: "Marker 5",
lat: 1,
lng: 1,
info: "Info Window 5"
},
{
title: "Marker 6",
lat: 1,
lng: 2,
info: "Info Window 6"
}
];
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: { lat: 10, lng: 10 },
zoom: 3,
minZoom: 1,
maxZoom: 16,
mapTypeControl: false,
streetViewControl: false,
scrollwheel: false
});
infoWin = new google.maps.InfoWindow();
var markers = locations.map(function (location, i) {
return addMarker(i);
});
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML =
'<select onchange=\'myclick(this.value);\' name="country" class="wpcf7-form-control wpcf7-select form-control" aria-invalid="false"><option value="Please choose your country...">Please choose your country...</option>' +
side_bar_html +
"</select>";
var markerCluster = new MarkerClusterer(map, [], {
imagePath: "https://www.takagi-j.com/test/marker-clusterer-example/images/m"
});
markerCluster.setStyles(
markerCluster.getStyles().map(function (style) {
style.textColor = "#fff";
return style;
})
);
markerCluster.addMarkers(markers);
}
function addMarker(i) {
infoWin = new google.maps.InfoWindow();
var draftMarker = locations[i];
var myLatLng = new google.maps.LatLng(draftMarker.lat, draftMarker.lng);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: draftMarker.title,
icon:
"https://www.takagi-j.com/test/marker-clusterer-example/images/location-marker.png"
});
google.maps.event.addListener(marker, "click", function (evt) {
infoWin.setContent(draftMarker.info);
infoWin.open(map, marker);
});
gmarkers.push(marker);
side_bar_html +=
"<option value=" +
(gmarkers.length - 1) +
">" +
draftMarker.title +
"</option>";
return marker;
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
map.setCenter(gmarkers[i].getPosition());
google.maps.event.trigger(gmarkers[i], "click");
}
google.maps.event.addDomListener(window, "load", initMap);