jquery - Adding links to Google Maps Markers outside of the map using options in a select dropdown
问题描述
I have a Google Map with Various locations and I want to be able to goto and open the Info Window when a link - outside of the map is clicked.
Below is the relevant bits of the working code, but I want to put the links into a select drop down rather than just text links in a div
$('#markers').append('<a class="marker-link" data-markerid="' + i + '" href="#">' + locations[i][1] + '</a> ');
google.maps.event.addListener(marker, 'click', (function (marker, i) {
$('.marker-link').on('click', function () {
google.maps.event.trigger(markers[$(this).data('markerid')], 'click');
});
BTW there is the following div in the HTML
<div id="markers"></div>
To try and get it to work as a drop down I have changed this line
$('#markers').append('<a class="marker-link" data-markerid="' + i + '" href="#">' + locations[i][1] + '</a> ');
to
$('#dropdown').append('<option data-markerid="' + i + '">' + locations[i][1] + '</option>');
and then added this into the HTML
<select id="dropdown"></select>
I then do get a drop down with all the various titles and each option has
data-markerid="0"
data-markerid="1"
and I also added
$( "#dropdown" ).change(function() {
google.maps.event.trigger(markers[$(this).data('markerid')], 'click');
});
But when I select an item from the drop down it does not go to and show the relevant InfoWindow
解决方案
实际上我最终以不同的方式执行此操作 - 但它达到了相同的结果 - 具有不同位置的 Google 地图和地图外部的下拉菜单,可用于导航到每个位置并显示 InfoWindow
var map;
var info = new google.maps.InfoWindow();
var gmarkers = [];
var side_bar_html = "";
function initialize() {
var mapOptions = {
center: {lat: 0, lng: 0},
...options...
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var markers = [
['Location 1', 0, 0, 'Description 1'],
['Location 2', 0, 1, 'Description 2'],
['Location 3', 1, 0, 'Description 3'],
...etc...
];
for (var i = 0; i < markers.length; i++) {
marker = addMarker(i);
}
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = "<select onchange='myclick(this.value);'>"+side_bar_html+"</select>";
function addMarker(i) {
var draftMarker = markers[i];
var myLatLng = new google.maps.LatLng(draftMarker[1], draftMarker[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: draftMarker[0]
});
google.maps.event.addListener(marker, 'click', function () {
info.setContent(draftMarker[3]);
info.open(map, marker);
});
gmarkers.push(marker);
side_bar_html += '<option value=' + (gmarkers.length-1) + '>' + draftMarker[0] + '<\/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', initialize);
然后将其添加到 HTML
<div id="side_bar"></div>
推荐阅读
- php - 将格式化的数据库查询从 MySQL 转换为 MySQLi
- r - R:将信息添加到宽格式数据并将其转换为长格式的最佳实践
- sql - 在单个语句中从多个表中删除行
- swift - 将 NSCompoundPredicate 与列表一起使用
- python - python 3.8中的导入错误:没有名为'_abc'的模块
- python - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (3,)
- build - 避免“方便的符号链接”并直接输出到工作区
- ios - iOS Charts Library - x轴值全部合并,怎么能单独显示,不缩放
- tcl - Tcl:容忍命令执行中的换行符
- auth0 - 是否可以恢复 Auth0 电子邮件模板?