Skip to content Skip to sidebar Skip to footer

How To Solve The Infobox Label Overlap On Multiple Markers On Google Map

I have multiple markers on a Google Map with a custom icon. The marker represents a number of items in that region. I have added an infobox which serves as a label of how many item

Solution 1:

You can use MarkerWithLabel

The "basic" example has multiple characters. Related question: Google maps Marker Label with multiple characters

code snippet:

functioninitMap() {
   var latLng = new google.maps.LatLng(49.47805, -123.84716);
   var homeLatLng = new google.maps.LatLng(49.47805, -123.84716);

   var map = new google.maps.Map(document.getElementById('map_canvas'), {
     zoom: 13,
     center: latLng,
     mapTypeId: google.maps.MapTypeId.ROADMAP
   });

   var marker1 = newMarkerWithLabel({
     position: homeLatLng,
     draggable: true,
     raiseOnDrag: true,
     map: map,
     labelContent: "$425K",
     labelAnchor: new google.maps.Point(22, 0),
     labelClass: "labels", // the CSS class for the labellabelStyle: {
       opacity: 0.75
     }
   });

   var marker2 = newMarkerWithLabel({
     position: new google.maps.LatLng(49.475, -123.84),
     draggable: true,
     raiseOnDrag: true,
     map: map,
     labelContent: "$395K",
     labelAnchor: new google.maps.Point(22, 0),
     labelClass: "labels", // the CSS class for the labellabelStyle: {
       opacity: 1.0
     }
   });

   var iw1 = new google.maps.InfoWindow({
     content: "Home For Sale"
   });
   var iw2 = new google.maps.InfoWindow({
     content: "Another Home For Sale"
   });
   google.maps.event.addListener(marker1, "click", function(e) {
     iw1.open(map, this);
   });
   google.maps.event.addListener(marker2, "click", function(e) {
     iw2.open(map, this);
   });
 }
 google.maps.event.addDomListener(window, 'load', initMap);
.labels {
  color: red;
  background-color: white;
  font-family: "Lucida Grande", "Arial", sans-serif;
  font-size: 10px;
  font-weight: bold;
  text-align: center;
  width: 40px;
  border: 2px solid black;
  white-space: nowrap;
}
<scriptsrc="http://maps.googleapis.com/maps/api/js"></script><scriptsrc="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script><divid="map_canvas"style="height: 400px; width: 100%;"></div><divid="log"></div>

Solution 2:

For people like me who finds this 2021.

Setting an increasing/decreasing zIndex on the markers will solve this problem. (depending on what marker you want on top)

Post a Comment for "How To Solve The Infobox Label Overlap On Multiple Markers On Google Map"