简体   繁体   English

google map 标记点击 function 不同外部链接的问题

[英]google map marker click function problem for different external links

I am using google map for my website.我正在为我的网站使用谷歌 map。 I would like to show different colors when clicking on the right menu of the map.我想在单击 map 的右侧菜单时显示不同的 colors。 each link has it's own marker color.每个链接都有自己的标记颜色。 If you click on the first link the marker must be green, if you click on the second color it must be red and go on.如果单击第一个链接,标记必须为绿色,如果单击第二个颜色,则标记必须为红色并且 go 亮。 But in the code, I write after I click on each of these links the marker gets the red color which is the last color I assign.但是在代码中,我在单击每个链接后编写,标记会变成红色,这是我分配的最后一种颜色。 I think that marker, 'click', function should be a different function for each link.我认为标记,“点击”,function 对于每个链接应该是不同的 function。 I've tried a lot of examples but I couldn't make it work.我已经尝试了很多例子,但我无法让它工作。

Here is the photo:这是照片: 在此处输入图像描述

Here is the HTML codes:这是 HTML 代码:

<div class="col-sm-12 col-xs-12 map-site">
        <span>نقشه لامپ های پارک ملت</span>
        <div class="sidemap col-sm-3 col-xs-3">
            <button type="button" class="navbar-toggle1 try-op" > 
              <span class="icon-bar top-m"></span> 
              <span class="icon-bar mid-m"></span> 
              <span class="icon-bar bottom-m"></span> 
            </button>
            <div class="menumap"> <!-- <span class="btnClose">×</span> -->
              <ul>
                <li><a class="healthmap" id="healthmap">سلامت بارتی</a></li>
                <li><a class="chargemap" id="chargemap">شارژ باتری</a></li>
                <li><a class="saturationmap" id="saturationmap">شدت روشنایی</a></li>
              </ul>
            </div>
        </div>
        <div id="map-canvas"></div>     
</div>

JS codes: JS代码:

var gmarkers = [];
var pmarkers = [];
var rmarkers = [];

function initialize() {
  var locations = [
    ['چراغ یک', 36.320153, 59.536075, 4],
    ['چراغ دو', 36.320014, 59.536612, 5],
    ['چراغ سه', 36.319859, 59.537212, 3],
    ['چراغ چهار', 36.320066, 59.538091, 2],
    ['چراغ پنج', 36.319513, 59.536440, 1]
  ];

  var mapOptions = {
    zoom: 17,
    center: new google.maps.LatLng(36.320020, 59.537801),
    mapTypeId: 'satellite'
  }

  var map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);
  for (i = 0; i < locations.length; i++) {
    //var image = 'image/marker.png';
    var myLatLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
    var marker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      icon: {
        url: "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png"
      }
    });
    marker.addListener('click', function(e) {
      this.setIcon({
        url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
      })
    })
    gmarkers.push(marker);


    marker.addListener('click', function(p) {
      this.setIcon({
        url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"
      })
    })
    pmarkers.push(marker);


    marker.addListener('click', function(r) {
      this.setIcon({
        url: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"
      })
    })
    rmarkers.push(marker);

  }

}

google.maps.event.addDomListener(window, 'load', initialize);

$('.healthmap').click(function() {
  for (var i = 0; i < gmarkers.length; i++) {
    google.maps.event.trigger(gmarkers[i], "click");
  }
});

$('.chargemap').click(function() {
  for (var i = 0; i < pmarkers.length; i++) {
    google.maps.event.trigger(pmarkers[i], "click");
  }
});

$('.saturationmap').click(function() {
  for (var i = 0; i < rmarkers.length; i++) {
    google.maps.event.trigger(rmarkers[i], "click");
  }
});

Can any one help me with this problem?任何人都可以帮我解决这个问题吗? I think It occurs because of the different click function and different marker.addListener('click') , But I can't fix this problem.我认为它的发生是因为不同的click function 和不同marker.addListener('click') ,但我无法解决这个问题。

here is the js fiddle link.是js小提琴链接。

To change all the markers and have different colors, change the icon in the external onclick functions.要更改所有标记并具有不同的 colors,请更改外部onclick函数中的图标。

$('.healthmap').click(function() {
  for (var i = 0; i < gmarkers.length; i++) {
    gmarkers[i].setIcon({
      url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
    });
  }
});

$('.chargemap').click(function() {
  for (var i = 0; i < gmarkers.length; i++) {
    gmarkers[i].setIcon({
      url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"
    });
  }
});

$('.saturationmap').click(function() {
  for (var i = 0; i < gmarkers.length; i++) {
    gmarkers[i].setIcon({
      url: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"
    })
  }
});

proof of concept fiddle概念证明小提琴

code snippet:代码片段:

 var gmarkers = []; function initialize() { var locations = [ ['چراغ یک', 36.320153, 59.536075, 4], ['چراغ دو', 36.320014, 59.536612, 5], ['چراغ سه', 36.319859, 59.537212, 3], ['چراغ چهار', 36.320066, 59.538091, 2], ['چراغ پنج', 36.319513, 59.536440, 1] ]; var mapOptions = { zoom: 17, center: new google.maps.LatLng(36.320020, 59.537801), mapTypeId: 'satellite' } var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); for (i = 0; i < locations.length; i++) { var myLatLng = new google.maps.LatLng(locations[i][1], locations[i][2]); var marker = new google.maps.Marker({ position: myLatLng, map: map, icon: { url: "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png" } }); gmarkers.push(marker); } } google.maps.event.addDomListener(window, 'load', initialize); $('.healthmap').click(function() { for (var i = 0; i < gmarkers.length; i++) { gmarkers[i].setIcon({ url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png" }); } }); $('.chargemap').click(function() { for (var i = 0; i < gmarkers.length; i++) { gmarkers[i].setIcon({ url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png" }); } }); $('.saturationmap').click(function() { for (var i = 0; i < gmarkers.length; i++) { gmarkers[i].setIcon({ url: "http://maps.google.com/mapfiles/ms/icons/red-dot.png" }) } });
 #map-canvas { height: 60%; } html, body { height: 100%; margin: 0; padding: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-sm-12 col-xs-12 map-site" style="height:100%; width: 100%;"> <span>نقشه لامپ های پارک ملت</span> <div class="sidemap col-sm-3 col-xs-3"> <button type="button" class="navbar-toggle1 try-op"> <span class="icon-bar top-m"></span> <span class="icon-bar mid-m"></span> <span class="icon-bar bottom-m"></span> </button> <div class="menumap"> <.-- <span class="btnClose">×</span> --> <ul> <li><a class="healthmap" id="healthmap">سلامت بارتی(make green)</a></li> <li><a class="chargemap" id="chargemap">شارژ باتری(make blue)</a></li> <li><a class="saturationmap" id="saturationmap">شدت روشنایی(make red)</a></li> </ul> </div> </div> <div id="map-canvas"></div> </div> <:-- Replace the value of the key parameter with your own API key. --> <script src="https.//maps?googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

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

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