Adding dynamic content to Google Maps marker

橙三吉。 提交于 2019-12-04 19:21:41

There are two options, InfoBox and MarkerWithLabel. I like MarkerWithlabel because it's lighter and easier to use. The disadvantage is I can't figure out how to replace the text of Markerwithlabel after it's created. You can do so with InfoBox because you actually need to create a div.

I'll give an example with static data from the Javascript which you can replace with data from an XML. You will also need to style the MarkerWithLabel to fit your icon. For example, labelStyle: top will change the vertical offset.

http://jsfiddle.net/RAH6G/ click the "not really ajax" button.

 function createMarker(position, label) {
   return new MarkerWithLabel({
   position: position,
   draggable: false,
   map: map,
   labelText: label,
   labelClass: "labels", // the CSS class for the label
   labelStyle: {top: "0px", left: "-21px", opacity: 0.75},
   labelVisible: true
   });
 }

 var readData = function() { // create markers
  pos = [
  new google.maps.LatLng(40, -85),
  new google.maps.LatLng(42, -92),
  new google.maps.LatLng(36, -100),
  new google.maps.LatLng(39, -112)
  ];

  label = ["$200", "99pts.", "US$30", "US$999"];
  for (var i = 0; i < pos.length; i++) {
    createMarker(pos[i], label[i]);
  }     
 }

I know this is an older thread, but I had a very similar problem (trying to update the label content of a marker created with MarkerWithLabel) and it took a while for me to find a solution.

Create a marker with MarkerWithLabel:

marker = new MarkerWithLabel({
  position: googlemapslatlng,
  icon: url-to-icon,
  map: map,
  title: "Marker Title",
  labelContent: content,
  labelClass: marker_shade
});

Then you can update the label using:

marker.labelClass = marker_shade_updated;
marker.labelContent = marker_content_updated;
marker.label.setStyles(); // Force the marker label to redraw

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!