我需要制作一个包含一些多边形的地图传单,我想根据MYSQL的数据为我的多边形着色。但是,当我尝试此代码时,我的多边形不显示。
这是我在map.php上的代码:
<?php
include 'koneksi.php';
$sql="select sum(skor_bobot) as hasilnya from penilaian where bulan=1 and id_kelurahan=1";
$data=mysql_query($sql);
$js='';
while ($row=mysql_fetch_array($data)) {
$js .='L.geoJson(states, {
style: function(feature) {
if ((feature.properties.party=='.'Republican'.')&&('.$row['hasilnya'].'=='.'10'.')) {
return {color: "#ffff89"};
} else {
return {color: "#ff0000"};
};
}
}).addTo(mymap);';
}
echo $js; ?>
以下是我的map.php上的geojson代码:
var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {"party": "Democrat"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}];
最佳答案
奇怪您在JS代码中没有(双引号)Republican
吗?
'(feature.properties.party=='.'"Republican"'.')'
如果没有这些双引号,则将代码作为JavaScript执行后,就传递了变量
Republican
(您以前没有定义,因此会出现控制台错误),而不是要与之比较的字符串"Republican"
。