我已经在服务器中实现了这个美国地图,但我正在尝试根据mysql响应更改某些状态的颜色,但我无法做到这一点
这是我的代码
<script type="text/javascript" src="includes/js/jquery-2.1.0.min.js">
</script>
<script src="includes/js/jquery.vmap.js"></script>
<script src="includes/js/jquery.vmap.canada.js"></script>
<script src="includes/js/jquery.vmap.usa.js"></script>
<script>
$(document).ready(function(){
var states_colors = {};
var states = {};
$.post("state_service.php", function(data) {
var states_data = data.split(',');
for (var i = 0; i < states_data.length; i++) {
states[i] = states_data[i].replace(/[""\[\]]+/g, '');
states_colors[i] = states[i]+': #8EE5EE,';
};
console.log(states_colors);
$('#vmap').vectorMap('set', 'colors', states_colors);
});
$('#vmap').vectorMap({
map: 'usa_en',
backgroundColor: null,
color: '#F58025',
hoverColor: '#754200',
selectedColor: '#754200',
enableZoom: false,
showTooltip: false,
onRegionClick: function(element, code)
{
var arr = [];
arr = code.split('-');
var url = 'search.php?';
var query = 'keywords=&city=&state='+arr[1];
window.location.href = url + query;
}
});
});
</script>
<div id="vmap" style="width: 600px; height: 600px;"></div>
这就是Json的回应带给我的是哪些是我想强调的状态
["AB","CO","UT"]
答案 0 :(得分:2)
根据以下文档正确的文档
$('#vmap').vectorMap('set', 'colors', states_colors);
但是setColors函数将key和color作为参数。所以我改为跟随,它开始工作
$('#vmap').vectorMap('set', 'colors', states[i], '#8EE5EE');
示例:http://codepen.io/anon/pen/RPjJYb
使用以下网站库
http://jqvmap.com/js/vmap/jquery.vmap.js?v=1.0
http://jqvmap.com/js/vmap/jquery.vmap.usa.js?v=1.0
答案 1 :(得分:1)
只需更改脚本序列
即可首先初始化vectormap
,然后调用状态颜色..
初始化vectormap
document.ready
第三次尝试
使state_color数组全局,然后在vectormap initalisation中调用数组
例如
states_colors = {};
states_colors ['AB'] = '#A4D886';
states_colors ['UT'] = '#FCECA2';
states_colors ['CO'] = '#8EE5EE ';
$('#vmap').vectorMap({
colors: states_colors,
map: 'usa_en',
backgroundColor: null,
hoverColor: '#754200',
selectedColor: '#754200',
enableZoom: false,
showTooltip: false,
onRegionClick: function(element, code)
{
var arr = [];
arr = code.split('-');
var url = 'search.php?';
var query = 'keywords=&city=&state='+arr[1];
window.location.href = url + query;
}
});
如果您可以复制下面的FIDDLE中的情况