Modify Marker Label CSS Property

7,358 views
Skip to first unread message

Buster

unread,
Aug 13, 2012, 5:56:10 PM8/13/12
to Google Maps JavaScript API v3
Hi,
If possible, I would like to dynamically modify the CSS width property
to reflect the width of the labelText string.
For example, the CSS set width property is 40px. The text string
property is 100px. Change width = 100px

<style type="text/css">
.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;
}
</style>

function createLabeledMarker(lat, lon, labelText, infoText,
iconURI) {
var html = "<span class=\"infoWindowContent\">" + infoText +
"</span>";
var thisMarker = new MarkerWithLabel({
map: map,
position: new google.maps.LatLng(lat, lon),
title: '', //mouseover popup label
icon: iconURI,
draggable: false,
raiseOnDrag: false,
labelContent: labelText,
labelVisible: true,
labelAnchor: new google.maps.Point(22, 0),
labelClass: "labels", // the CSS class for the label
labelStyle: { opacity: 1.00 }
});

changeWidth(labelText.length);

bindInfoWindow(thisMarker, map, infoWindow, html);
gmarkers_list.push(thisMarker);
}


function changeWidth(newWidth) {
var elem = document.getElementById("labels");
elem.width = newWidth;
}

I've tried this, but no joy.

appreciate any assistance.

Reference link:
http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.5/examples/basic.html

thanks.



ProbablyMike

unread,
Aug 14, 2012, 4:22:15 AM8/14/12
to google-map...@googlegroups.com
From you code snippet it's impossible to say if you have an element with the ID of labels, which is what you are looking for in the changeWidth function.

You are setting a CLASS for the "labelClass" on the marker, maybe the ID is set elsewhere?
Please provide a link to your page.

Buster

unread,
Aug 14, 2012, 9:16:24 AM8/14/12
to Google Maps JavaScript API v3
www.gmapsapi.net/test

I also tried setting the CSS to ID to #div1, .labels { ... }
using
var elem = document.getElementById("div1");
as well as trying
var elem = document.getElementsByClassName("labels");

Thanks for any assistance.

Buster

unread,
Aug 15, 2012, 12:57:14 AM8/15/12
to Google Maps JavaScript API v3
Problem solved. Can now dynamically change the Marker Label CSS class
properties, i.e., label width, color, font, font-size, etc.
Reply all
Reply to author
Forward
0 new messages