简体   繁体   English

Google地图:点击按钮即可打开特定的信息窗口

[英]Google map: Open specific Info window on button click

I have used google map custom marker with info windows. 我已经使用了带有信息窗口的Google Map自定义标记。 I need to add function to open Info window when user click on specific button . 当用户单击特定按钮时,我需要添加功能以打开“信息”窗口 There are multiple markers with separate contents. 有多个带有单独内容的标记。 Here i found a solution to open a single Info window with a single button click: https://stackoverflow.com/a/18334899/6191987 在这里,我找到了一种通过单击一个按钮即可打开单个“信息”窗口的解决方案: https : //stackoverflow.com/a/18334899/6191987

I have tried with the above solution, but i don't know how to target to open each specific Info windows on button click. 我已经尝试了上述解决方案,但是我不知道如何以单击按钮为目标来打开每个特定的信息窗口。

Here is the code what i have tried: 这是我尝试过的代码:

  html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } .btns { display: inline-block; width: 100%; margin-bottom: 20px } .btns a { display: block; padding: 10px; float: left; background: #eee; margin-right: 5px; text-decoration: none; } 
 <div class="btns"> <a href="#" onclick="myClick(0);">Open Info Window 1</a> <a href="#" onclick="myClick(0);">Open Info Window 2</a> <a href="#" onclick="myClick(0);">Open Info Window 3</a> </div> <div id="map"></div> <script> var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 17, center: new google.maps.LatLng(40.712696, -74.005019), mapTypeId: 'roadmap', styles: [{ elementType: 'geometry', stylers: [{ color: '#242f3e' }] }, { elementType: 'labels.text.stroke', stylers: [{ color: '#242f3e' }] }, { elementType: 'labels.text.fill', stylers: [{ color: '#746855' }] }, { featureType: 'administrative.locality', elementType: 'labels.text.fill', stylers: [{ color: '#d59563' }] }, { featureType: 'poi', elementType: 'labels.text.fill', stylers: [{ color: '#d59563' }] }, { featureType: 'poi.park', elementType: 'geometry', stylers: [{ color: '#263c3f' }] }, { featureType: 'poi.park', elementType: 'labels.text.fill', stylers: [{ color: '#6b9a76' }] }, { featureType: 'road', elementType: 'geometry', stylers: [{ color: '#38414e' }] }, { featureType: 'road', elementType: 'geometry.stroke', stylers: [{ color: '#212a37' }] }, { featureType: 'road', elementType: 'labels.text.fill', stylers: [{ color: '#9ca5b3' }] }, { featureType: 'road.highway', elementType: 'geometry', stylers: [{ color: '#746855' }] }, { featureType: 'road.highway', elementType: 'geometry.stroke', stylers: [{ color: '#1f2835' }] }, { featureType: 'road.highway', elementType: 'labels.text.fill', stylers: [{ color: '#f3d19c' }] }, { featureType: 'transit', elementType: 'geometry', stylers: [{ color: '#2f3948' }] }, { featureType: 'transit.station', elementType: 'labels.text.fill', stylers: [{ color: '#d59563' }] }, { featureType: 'water', elementType: 'geometry', stylers: [{ color: '#17263c' }] }, { featureType: 'water', elementType: 'labels.text.fill', stylers: [{ color: '#515c6d' }] }, { featureType: 'water', elementType: 'labels.text.stroke', stylers: [{ color: '#17263c' }] }] }); var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; var icons = { parking: { icon: iconBase + 'parking_lot_maps.png' }, library: { icon: iconBase + 'library_maps.png' }, info: { icon: iconBase + 'info-i_maps.png' } }; var features = [{ position: new google.maps.LatLng(40.712696, -74.005019), content: 'test content one', type: 'parking' }, { position: new google.maps.LatLng(40.712753, -74.006081), content: 'test content two', type: 'parking' }, { position: new google.maps.LatLng(40.713664, -74.007819), content: 'test content three <a href="http://www.google.com">A link to google</a>', type: 'library' }]; var infowindow = new google.maps.InfoWindow({ content: "" }); // Create markers. features.forEach(function(feature) { var marker = new google.maps.Marker({ position: feature.position, content: feature.content, icon: icons[feature.type].icon, map: map }); var content = "<a href='" + feature.content + "'>A link to google</a>"; marker.addListener('click', function() { infowindow.setContent('<div><strong>' + marker.content + '</strong></div>'); infowindow.open(map, marker); }); }); } //on click function function myClick(id) { google.maps.event.trigger(markers[id], 'click'); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap"> </script> 

JSFiddle: https://jsfiddle.net/vishnuprasadps/3wk7q3fd/ JSFiddle: https ://jsfiddle.net/vishnuprasadps/3wk7q3fd/

That is because markers is not a defined variable in your code. 这是因为markers不是代码中的已定义变量。 At the top of your code, you can simply declare markers as an empty array, ie: 在代码的顶部,您可以简单地将markers声明为空数组,即:

var markers = [];

Further down close to the bottom of your code, when you are iterating through your features collection, you have to push the marker object into this array, ie: markers.push(marker); 在代码底部附近,当您遍历features集合时,必须将标记对象推入此数组,即: markers.push(marker); . In the context of your code, it will look something like this: 在您的代码的上下文中,它将看起来像这样:

// Create markers.
features.forEach(function(feature) {
  var marker = new google.maps.Marker({
    position: feature.position,
    content: feature.content,
    icon: icons[feature.type].icon,
    map: map
  });
  var content = "<a href='" + feature.content + "'>A link to google</a>";
  marker.addListener('click', function() {
    infowindow.setContent('<div><strong>' + marker.content + '</strong></div>');

    infowindow.open(map, marker);
  });

  // Push your marker object into your array
  markers.push(marker);
});

And that should work: see proof-of-concept below. 那应该起作用:请参见下面的概念验证。 Note: I have taken the liberty to change the inline JS callback arguments, so that you will not trigger the same marker info window for all 3 links. 注意:我已经自由更改了内联JS回调参数,因此您不会为所有3个链接触发相同的标记信息窗口。

 html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } .btns { display: inline-block; width: 100%; margin-bottom: 20px } .btns a { display: block; padding: 10px; float: left; background: #eee; margin-right: 5px; text-decoration: none; } 
 <div class="btns"> <a href="#" onclick="myClick(0);">Open Info Window 1</a> <a href="#" onclick="myClick(1);">Open Info Window 2</a> <a href="#" onclick="myClick(2);">Open Info Window 3</a> </div> <div id="map"></div> <script> var map; var markers = []; function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 17, center: new google.maps.LatLng(40.712696, -74.005019), mapTypeId: 'roadmap', styles: [{ elementType: 'geometry', stylers: [{ color: '#242f3e' }] }, { elementType: 'labels.text.stroke', stylers: [{ color: '#242f3e' }] }, { elementType: 'labels.text.fill', stylers: [{ color: '#746855' }] }, { featureType: 'administrative.locality', elementType: 'labels.text.fill', stylers: [{ color: '#d59563' }] }, { featureType: 'poi', elementType: 'labels.text.fill', stylers: [{ color: '#d59563' }] }, { featureType: 'poi.park', elementType: 'geometry', stylers: [{ color: '#263c3f' }] }, { featureType: 'poi.park', elementType: 'labels.text.fill', stylers: [{ color: '#6b9a76' }] }, { featureType: 'road', elementType: 'geometry', stylers: [{ color: '#38414e' }] }, { featureType: 'road', elementType: 'geometry.stroke', stylers: [{ color: '#212a37' }] }, { featureType: 'road', elementType: 'labels.text.fill', stylers: [{ color: '#9ca5b3' }] }, { featureType: 'road.highway', elementType: 'geometry', stylers: [{ color: '#746855' }] }, { featureType: 'road.highway', elementType: 'geometry.stroke', stylers: [{ color: '#1f2835' }] }, { featureType: 'road.highway', elementType: 'labels.text.fill', stylers: [{ color: '#f3d19c' }] }, { featureType: 'transit', elementType: 'geometry', stylers: [{ color: '#2f3948' }] }, { featureType: 'transit.station', elementType: 'labels.text.fill', stylers: [{ color: '#d59563' }] }, { featureType: 'water', elementType: 'geometry', stylers: [{ color: '#17263c' }] }, { featureType: 'water', elementType: 'labels.text.fill', stylers: [{ color: '#515c6d' }] }, { featureType: 'water', elementType: 'labels.text.stroke', stylers: [{ color: '#17263c' }] }] }); var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; var icons = { parking: { icon: iconBase + 'parking_lot_maps.png' }, library: { icon: iconBase + 'library_maps.png' }, info: { icon: iconBase + 'info-i_maps.png' } }; var features = [{ position: new google.maps.LatLng(40.712696, -74.005019), content: 'test content one', type: 'parking' }, { position: new google.maps.LatLng(40.712753, -74.006081), content: 'test content two', type: 'parking' }, { position: new google.maps.LatLng(40.713664, -74.007819), content: 'test content three <a href="http://www.google.com">A link to google</a>', type: 'library' }]; var infowindow = new google.maps.InfoWindow({ content: "" }); // Create markers. features.forEach(function(feature) { var marker = new google.maps.Marker({ position: feature.position, content: feature.content, icon: icons[feature.type].icon, map: map }); var content = "<a href='" + feature.content + "'>A link to google</a>"; marker.addListener('click', function() { infowindow.setContent('<div><strong>' + marker.content + '</strong></div>'); infowindow.open(map, marker); }); markers.push(marker); }); } //on click function function myClick(id) { google.maps.event.trigger(markers[id], 'click'); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvkk7wNQcIYXZ7S8XNG8cG-elq0QE2v3k&callback=initMap"> </script> 


Additional notes: 补充笔记:

  • Do not use inline JS . 不要使用内联JS You can simply iterate through your links and bind event listeners to them using .addEventListener('click', function() {...}) . 您可以简单地遍历链接,并使用.addEventListener('click', function() {...})将事件侦听器绑定到它们。
  • If you have an arbitrary number of features, it helps that you get the length of this collection and generate the links dynamically 如果您具有任意数量的功能,则可以帮助您获取此集合的长度并动态生成链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM