jvectormap - 定义可选区域
jvectormap - define selectable regions
是否可以在 jVectorMaps 中定义可以 selected 的静态区域?
我只需要定义允许用户访问的 6 个区域 select.
棘手的部分是,我需要将欧洲、亚洲和世界作为一个区域以及 "Poland" 和 "Canada"。
如果用户 select 在波兰,它应该 select 只是 "Poland" 但如果用户 select 在 "Europe" 的任何其他国家/地区,它应该 selected 所有欧洲国家。
jvectormaps 可以吗?
jVectorMap 区域是由 2 个字母的 ISO 国家代码标识的 SVG 路径。
您可以不合并那些路径,但您可以将那些国家代码收集到宏区域中,并使用这组代码一次 select 您需要的所有 jVectorMap 区域。
这是一个包含 4 个宏观区域的示例:波兰、加拿大、欧洲和世界其他地区。
$(document).ready(function () {
// Group states into Areas
var areas = [];
areas[0] = [];
areas[1] = ["PL"];
areas[2] = ["BE","FR","BG","DK","HR","DE","BA","HU","FI","BY","GR","NL","PT","NO","LV","LT","LU","XK","CH","EE","IS","AL","IT","CZ","GB","IE","ES","ME","MD","RO","RS","MK","SK","SI","UA","SE","AT"];
areas[3] = ["CA"];
function selectArea(code){
var mapObj = $("#map").vectorMap("get", "mapObject");
areas.forEach(function(area) {
if(area.indexOf(code)>-1) {
mapObj.setSelectedRegions(area);
return;
}
});
}
function clearAll(){
var mapObj = $("#map").vectorMap("get", "mapObject");
mapObj.clearSelectedRegions();
}
$("#map").vectorMap({
map: "world_mill",
backgroundColor: "aliceblue",
zoomOnScroll: true,
regionsSelectable: true,
regionStyle: {
initial: {
fill: "lightgrey"
},
selected: {
fill: "darkseagreen"
}
},
onRegionClick: function(e, code){
clearAll();
selectArea(code);
return false;
}
});
(function () {
// Collect the rest of the World
var mapObj = $("#map").vectorMap("get", "mapObject");
var states = areas.join(",");
for(var code in mapObj.regions) {
if(mapObj.regions.hasOwnProperty(code)) {
if(states.indexOf(code) == -1) {
areas[0].push(code);
}
}
}
})();
});
<html>
<head>
<title>jVectorMap Areas</title>
<link rel="stylesheet" href="http://jvectormap.com/css/jquery-jvectormap-2.0.3.css" type="text/css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://jvectormap.com/js/jquery-jvectormap-2.0.3.min.js"></script>
<script src="http://jvectormap.com/js/jquery-jvectormap-world-mill.js"></script>
</head>
<body>
<div id="map" style="width: 600px; height: 400px"></div>
</body>
</html>
是否可以在 jVectorMaps 中定义可以 selected 的静态区域?
我只需要定义允许用户访问的 6 个区域 select.
棘手的部分是,我需要将欧洲、亚洲和世界作为一个区域以及 "Poland" 和 "Canada"。
如果用户 select 在波兰,它应该 select 只是 "Poland" 但如果用户 select 在 "Europe" 的任何其他国家/地区,它应该 selected 所有欧洲国家。
jvectormaps 可以吗?
jVectorMap 区域是由 2 个字母的 ISO 国家代码标识的 SVG 路径。
您可以不合并那些路径,但您可以将那些国家代码收集到宏区域中,并使用这组代码一次 select 您需要的所有 jVectorMap 区域。
这是一个包含 4 个宏观区域的示例:波兰、加拿大、欧洲和世界其他地区。
$(document).ready(function () {
// Group states into Areas
var areas = [];
areas[0] = [];
areas[1] = ["PL"];
areas[2] = ["BE","FR","BG","DK","HR","DE","BA","HU","FI","BY","GR","NL","PT","NO","LV","LT","LU","XK","CH","EE","IS","AL","IT","CZ","GB","IE","ES","ME","MD","RO","RS","MK","SK","SI","UA","SE","AT"];
areas[3] = ["CA"];
function selectArea(code){
var mapObj = $("#map").vectorMap("get", "mapObject");
areas.forEach(function(area) {
if(area.indexOf(code)>-1) {
mapObj.setSelectedRegions(area);
return;
}
});
}
function clearAll(){
var mapObj = $("#map").vectorMap("get", "mapObject");
mapObj.clearSelectedRegions();
}
$("#map").vectorMap({
map: "world_mill",
backgroundColor: "aliceblue",
zoomOnScroll: true,
regionsSelectable: true,
regionStyle: {
initial: {
fill: "lightgrey"
},
selected: {
fill: "darkseagreen"
}
},
onRegionClick: function(e, code){
clearAll();
selectArea(code);
return false;
}
});
(function () {
// Collect the rest of the World
var mapObj = $("#map").vectorMap("get", "mapObject");
var states = areas.join(",");
for(var code in mapObj.regions) {
if(mapObj.regions.hasOwnProperty(code)) {
if(states.indexOf(code) == -1) {
areas[0].push(code);
}
}
}
})();
});
<html>
<head>
<title>jVectorMap Areas</title>
<link rel="stylesheet" href="http://jvectormap.com/css/jquery-jvectormap-2.0.3.css" type="text/css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://jvectormap.com/js/jquery-jvectormap-2.0.3.min.js"></script>
<script src="http://jvectormap.com/js/jquery-jvectormap-world-mill.js"></script>
</head>
<body>
<div id="map" style="width: 600px; height: 400px"></div>
</body>
</html>