最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mouseover, mouseout and click on the same marker on a google map - Stack Overflow

programmeradmin0浏览0评论

When I hover over mouse then an InfoWindow should open saying "hello". However if I take cursor elsewhere it should close that InfoWindow. This is possible using the code below:

google.maps.event.addListener(marker, 'mouseover', function() {
   infowindow.setContent("Hello");
   infowindow.open(map, marker);
});

google.maps.event.addListener(marker, 'mouseout', function() {        
   infowindow.close();
});

But if I click on the marker it should open the InfoWindow with different content in it. This is possible using the code below:

google.maps.event.addListener(marker, 'click',function() {        
   infowindow.setContent(mycontent(name,msg));
   infowindow.open(map, marker);          
});

Problem: If I click on the marker the InfoWindow opens with my content but as soon as cursor moves the InfoWindow closes due to mouseout event. How do we prevent this so that on click, InfoWindow remains open until and unless some one clicks on the cross (X) mark on InfoWindow?

When I hover over mouse then an InfoWindow should open saying "hello". However if I take cursor elsewhere it should close that InfoWindow. This is possible using the code below:

google.maps.event.addListener(marker, 'mouseover', function() {
   infowindow.setContent("Hello");
   infowindow.open(map, marker);
});

google.maps.event.addListener(marker, 'mouseout', function() {        
   infowindow.close();
});

But if I click on the marker it should open the InfoWindow with different content in it. This is possible using the code below:

google.maps.event.addListener(marker, 'click',function() {        
   infowindow.setContent(mycontent(name,msg));
   infowindow.open(map, marker);          
});

Problem: If I click on the marker the InfoWindow opens with my content but as soon as cursor moves the InfoWindow closes due to mouseout event. How do we prevent this so that on click, InfoWindow remains open until and unless some one clicks on the cross (X) mark on InfoWindow?

Share Improve this question edited Jan 14, 2016 at 5:42 geocodezip 161k14 gold badges227 silver badges255 bronze badges asked Jan 14, 2016 at 4:54 kumar0981kumar0981 492 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Set a flag (I'll call it clicked) when the marker is clicked. Check that flag in the mouseover/mouseout listeners, don't take any action if it is set. Clear the flag (set it to false) when the InfoWindow is closed by clicking on the "X".

code snippet:

var infowindow = new google.maps.InfoWindow();
var clicked = false;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  });
  google.maps.event.addListener(marker, 'mouseover', function() {
    if (!clicked) {
      infowindow.setContent("Hello");
      infowindow.open(map, marker);
    }
  });

  google.maps.event.addListener(marker, 'mouseout', function() {
    if (!clicked) {
      infowindow.close();
    }
  });

  google.maps.event.addListener(marker, 'click', function() {
    clicked = true;
    infowindow.setContent("mycontent(name,msg)");
    infowindow.open(map, marker);
  });
  google.maps.event.addListener(infowindow,
    'closeclick',
    function() {
      clicked = false;
    })
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis./maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

发布评论

评论列表(0)

  1. 暂无评论