[英]Google Maps API - Getting opacity for polygon from geoJSON
I'm trying to make a map with a lot of polygons (11 000), and I want each polygon to have the same color, but with different opacity.我正在尝试制作包含大量多边形(11 000)的地图,并且我希望每个多边形具有相同的颜色,但具有不同的不透明度。 The opacity/polygons are stored like this:
不透明度/多边形存储如下:
{'geometry': {'coordinates': [[[10.927456267537572, 45.68179119797432],
[10.940290010697588, 45.68157387892596],
[10.939979018768243, 45.67257819153854],
[10.927147329501077, 45.672795442796335],
[10.927456267537572, 45.68179119797432]]],
'type': 'Polygon'},
'id': 1,
'properties': {'cellId': 39},
'style': {'opacity': 0.38888888888888884},
'type': 'Feature'}
And I want to have the polygon with id 1 to have the opacity stored in the line 'style': {'opacity': 0.38888888888888884},
, and the polygon with id 2 to have a different opacity etc. By default the polygons get shown, but with the same opacity.我想让 id 为 1 的多边形将不透明度存储在
'style': {'opacity': 0.38888888888888884},
,而 id 为 2 的多边形具有不同的不透明度等。默认情况下,显示多边形,但具有相同的不透明度。 I'm currently trying:我目前正在尝试:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: {lat: 46.0840601, lng: 11.1428063}
});
map.data.loadGeoJson(
'trentino-grid.geojson');
}
map.data.setStyle(function(feature) { //error here
var value = feature.getProperty('opacity');
var opacity = value;
return {
fillOpacity: opacity,
strokeWeight: opacity
};
});
But this doesn't work.但这不起作用。 I get an error for the line marked with the comment:
Cannot read property 'data' of undefined
.对于标有注释的行,我收到一个错误:
Cannot read property 'data' of undefined
。 How can I make the error go away and have all these polygons have their own opacity based on opacity
in the geoJSON?我怎样才能让错误消失并让所有这些多边形根据 geoJSON 中的不透明度有自己的不
opacity
? Thank you!谢谢!
you have the opacity in the wrong place, it isn't in the style object, it should be in the properties object:你在错误的地方设置了不透明度,它不在样式对象中,它应该在属性对象中:
{
'geometry': {
'coordinates': [
[
[10.927456267537572, 45.68179119797432],
[10.940290010697588, 45.68157387892596],
[10.939979018768243, 45.67257819153854],
[10.927147329501077, 45.672795442796335],
[10.927456267537572, 45.68179119797432]
]
],
'type': 'Polygon'
},
'id': 1,
'properties': {
'cellId': 39,
'opacity': 0.1
},
'type': 'Feature'
};
proof of concept fiddle概念证明小提琴
code snippet:代码片段:
var geocoder; var map; function initMap() { map = new google.maps.Map(document.getElementById('map'), { zoom: 12, center: { lat: 45.681, lng: 10.927 } }); map.data.addGeoJson(geoJson); map.data.setStyle(function(feature) { //error here var value = feature.getProperty('opacity'); var opacity = value; return { fillOpacity: opacity, fillColor: "#00FF00", strokeColor: '#00FF00', strokeWeight: 1, strokeOpacity: opacity }; }); google.maps.event.addDomListener(document.getElementById('btn'), "click", function(evt) { geoJson.properties.opacity = parseFloat(document.getElementById('opacity').value); map.data.addGeoJson(geoJson); }); } google.maps.event.addDomListener(window, "load", initMap); var geoJson = { 'geometry': { 'coordinates': [ [ [10.927456267537572, 45.68179119797432], [10.940290010697588, 45.68157387892596], [10.939979018768243, 45.67257819153854], [10.927147329501077, 45.672795442796335], [10.927456267537572, 45.68179119797432] ] ], 'type': 'Polygon' }, 'id': 1, 'properties': { 'cellId': 39, 'opacity': 0.1 }, 'type': 'Feature' };
html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px }
<script src="https://maps.googleapis.com/maps/api/js"></script> <input id="opacity" value="0.8" /> <input type="button" id="btn" value="set Opacity" /> <div id="map"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.