Janie Lee
Janie Lee

Reputation: 29

How to get current fillColor of google maps polygon?

I'm looking for a way to get the current fillColor of polygons with google maps api.

map.data.forEach(function(entry) {
  console.log(entry.getProperty('name'));
});

Which gives me the name associated with each polygon according to the geoJSON data.

It's pretty clear how to set style properties for these polygons but I don't see any way in the documentation to get the current fillColor.

Upvotes: 0

Views: 2021

Answers (1)

Matej P.
Matej P.

Reputation: 5383

You cannot retrieve style of individual features of google.maps.data layer. You can retrieve the global style of data layer, which you previously assigned using map.data.setStyle({StylingObject}) like this:

map.data.getStyle();

If you assigned styles using styling function (map.data.setStyle(function(entry){..StylingFunction..})) you cannot get the styles, but you should be able to determine them based on the StylingFunction you provided and properties of the current feature.

Also you cannot get styles assigned using map.data.overrideStyle(feature, {..}). But you can set properties with the same name to the feature when you assign style to it, and then retrieve the properties, not the styling. So you would have:

map.data.overrideStyle(feature, {
   fillColor:red,
   opacity:0.5
});
feature.setProperty('fillColor', 'red');
feature.setProperty('opacity', '0.5');

and when you want to retrieve the styles then you can go with:

map.data.forEach(function(entry) {
  console.log(entry.getProperty('fillColor')); //.. or opacity, or anything you set with setProperty
});

Upvotes: 2

Related Questions