通过单击特定标记的信息窗口上的按钮,我希望标记根据我单击的按钮来更改颜色 [英] By clicking a button, that's on a infowindow of a specific marker, I want the marker to change color depending on which button I click

查看:56
本文介绍了通过单击特定标记的信息窗口上的按钮,我希望标记根据我单击的按钮来更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mvc处理ASP.NET的视图.我使用带有不同标记的Google地图.这些标记中的每一个都有自己的信息窗口,每个信息窗口都有3个按钮.通过单击特定按钮,我希望标记更改颜色.我该怎么办?

I'm working on a view of the ASP.NET, using mvc. I using google maps with different markers. Each of those markers have their own infowindows that have 3 buttons each. By clicking a specific button I want the marker to change color. How can I do that?

我已经尝试使用document.getElementById和addEventListner.我还尝试使用onclick函数对按钮进行编程.

I have already tried using document.getElementById and using the addEventListner. I also tried to program the button by using the onclick function.

        var locations = [
            ["Recusado", "13/02/2019", 38.776816, -9.441833, 335, "foto.jpg", ],
            ["Aceite", "15/08/2019",38.817562, -8.941728, 36, "foto.jpg"],
            ["Em avaliação", "20/07/2019",38.487978, -9.004425, 90,"foto.jpg"],
            ["Concluído", "01/07/2019",37.045804, -8.898041, 12, "foto.jpg"]

        ];



 var infowindow =  new google.maps.InfoWindow({});





        function setMarkers(map) {

            for (var i = 0; i < locations.length; i++) {
                var fire = locations[i];
                var marker ;

                var contentString = '<div id="content">' +
                    '<div id="siteNotice">' +
                    '</div>' +
                    '<div id="bodyContent">' +
                    '<p><b>Avaliação da Ocorrência:</b></p>' +
                    '<p>Fotografias:' + fire[5] + '</p>' +
                    '<p>Avaliação:' + fire[0] + '</p>' +
                    '<p>Data:' + fire[1] + '</p></br>' +
                    '<button id="aceite">Aceite</button>' +
                    '<button>Recusado</button>' +
                    '<button> Em avaliação</button>' +
                    '<button> Concluído</button>' +
                    '</div>';
                var infoWindow = new google.maps.InfoWindow({ content: contentString });







                    marker = new google.maps.Marker({
                        position: { lat: fire[2], lng: fire[3] },
                        map: map,
                        info: contentString,
                        icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'

                    });



                google.maps.event.addListener(marker, 'click', function () {

                    infoWindow.setContent(this.info);
                    infoWindow.open(map, this);

                });

            }
        }

按钮以变量contentString表示,当我单击ID为"Aceite"的按钮时,我想将标记的颜色更改为绿色.

The buttons are represented in the variable contentString, and when I click the button with the id "Aceite", I want to change the color of the marker to green.

尝试将标记更改为绿色:

Attempt to change the marker to green:

<button id="aceite" onclick="state(marker)">Aceite</button> 

function state(marker){ 
  marker.setIcon('google.com/mapfiles/marker_green.png');
}

推荐答案

最简单的方法是在打开InfoWindow时(当您知道与之关联的标记时)添加点击侦听器功能.

Simplest would be to add the click listener function when you open the InfoWindow (when you know what marker it is associated with).

  1. 当InfoWindow的domready事件触发(并且已将其添加到DOM中)时,将点击侦听器添加到按钮:
  1. Add the click listener to the button when the domready event of the InfoWindow fires (and it has been added to the DOM):

google.maps.event.addListener(infoWindow, 'domready', function() {
  google.maps.event.addDomListener(document.getElementById("aceite"), 'click', function(e) {
    marker.setIcon('http://maps.google.com/mapfiles/marker_green.png');
  })
});

  1. 在标记单击侦听器中的标记上使用函数闭包(this在click事件侦听器功能中引用该标记,但不在InfoWindow domready事件侦听器中引用该标记,将其保存以便可以在此处使用它):
  1. Use function closure on the marker in the marker click listener (this references the marker in the click event listener function, but not in the InfoWindow domready event listener, save it so you can use it there):

google.maps.event.addListener(marker, 'click', function() {
  var marker = this;
  infoWindow.setContent(this.info);
  google.maps.event.addListener(infoWindow, 'domready', function() {
    google.maps.event.addDomListener(document.getElementById("aceite"), 'click', function(e) {
      marker.setIcon('http://maps.google.com/mapfiles/marker_green.png');
    })
  });
  infoWindow.open(map, this);
});

概念提琴证明

代码段:

function initMap() {
  var myLatLng = {
    lat: -25.363,
    lng: 131.044
  };

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: myLatLng
  });
  var infowindow = new google.maps.InfoWindow({});
  setMarkers(map);
}
var locations = [
  ["Recusado", "13/02/2019", 38.776816, -9.441833, 335, "foto.jpg", ],
  ["Aceite", "15/08/2019", 38.817562, -8.941728, 36, "foto.jpg"],
  ["Em avaliação", "20/07/2019", 38.487978, -9.004425, 90, "foto.jpg"],
  ["Concluído", "01/07/2019", 37.045804, -8.898041, 12, "foto.jpg"]
];

function setMarkers(map) {
  var bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < locations.length; i++) {
    var fire = locations[i];
    var marker;

    var contentString = '<div id="content">' +
      '<div id="siteNotice">' +
      '</div>' +
      '<div id="bodyContent">' +
      '<p><b>Avaliação da Ocorrência:</b></p>' +
      '<p>Fotografias:' + fire[5] + '</p>' +
      '<p>Avaliação:' + fire[0] + '</p>' +
      '<p>Data:' + fire[1] + '</p></br>' +
      '<button id="aceite">Aceite</button>' +
      '<button>Recusado</button>' +
      '<button> Em avaliação</button>' +
      '<button> Concluído</button>' +
      '</div>';
    var infoWindow = new google.maps.InfoWindow({
      content: contentString
    });

    marker = new google.maps.Marker({
      position: {
        lat: fire[2],
        lng: fire[3]
      },
      map: map,
      info: contentString,
      icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
    });
    bounds.extend(marker.getPosition());
    google.maps.event.addListener(marker, 'click', function() {
      var marker = this;
      infoWindow.setContent(this.info);
      google.maps.event.addListener(infoWindow, 'domready', function() {
        google.maps.event.addDomListener(document.getElementById("aceite"), 'click', function(e) {
          marker.setIcon('http://maps.google.com/mapfiles/marker_green.png');
        })
      });
      infoWindow.open(map, this);
    });
  }
  map.fitBounds(bounds);
}

html,
body,
#map {
  height: 100%;
  margin: 0;
  padding: 0;
}

<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

这篇关于通过单击特定标记的信息窗口上的按钮,我希望标记根据我单击的按钮来更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆