我已经到处寻找这个问题的答案,但最接近的结果并不令人满意(这个)。是否有办法让谷歌地图看起来和操作起来像jvectormap?我所指的操作是悬停在国家上等,外观方面则是指jvectormap拥有的整洁外观。
如我在评论中建议的那样,您可以查看如何样式化地图:
https://developers.google.com/maps/documentation/javascript/styling
这可以帮助您了解它的工作原理,并最终让您构建自己的内容: Google地图样式向导 关于融合表,一旦您找到适当的数据集(有许多,有些不完整,更或者少,几何细节的水平可以因数据集而异),您可以将其下载为CSV,并将其导入到您的数据库中。从那里,您可以查询您的数据库并为每个国家创建多边形。我稍后会更新我的答案,提供一些代码来帮助您入门。编辑:这是我在一个项目中使用的数据集。也许它能帮到你。它只包含我感兴趣的字段,但每个国家都有随机颜色关联。http://www.sendspace.com/file/plgku3
https://www.dropbox.com/s/7o5y26gfim1asf0/gmaps_countries.sql?dl=1
https://drive.google.com/file/d/1Qi4TOA3YUh3bL8SuIWbjA0B0QFIrA1ti/view?usp=sharing
编辑2:这是JavaScript代码:
var g = google.maps;
var countries = [];
function jsonCountries() {
$.ajax({
url : 'get_countries.php',
cache : true,
dataType : 'json',
async : true,
success : function(data) {
if (data) {
$.each(data, function(id,country) {
var countryCoords;
var ca;
var co;
if ('multi' in country) {
var ccArray = [];
for (var t in country['xml']['Polygon']) {
countryCoords = [];
co = country['xml']['Polygon'][t]['outerBoundaryIs']['LinearRing']['coordinates'].split(' ');
for (var i in co) {
ca = co[i].split(',');
countryCoords.push(new g.LatLng(ca[1], ca[0]));
}
ccArray.push(countryCoords);
}
createCountry(ccArray,country);
} else {
countryCoords = [];
co = country['xml']['outerBoundaryIs']['LinearRing']['coordinates'].split(' ');
for (var j in co) {
ca = co[j].split(',');
countryCoords.push(new g.LatLng(ca[1], ca[0]));
}
createCountry(countryCoords,country);
}
});
toggleCountries();
}
}
});
}
function createCountry(coords, country) {
var currentCountry = new g.Polygon({
paths: coords,
strokeColor: 'white',
strokeOpacity: 1,
strokeWeight: 1,
fillColor: country['color'],
fillOpacity: .6
});
countries.push(currentCountry);
}
function toggleCountries() {
for (var i=0; i<countries.length; i++) {
if (countries[i].getMap() !== null) {
countries[i].setMap(null);
} else {
countries[i].setMap(map);
}
}
}
这里是 get_countries.php:
$results = array();
$sql = "SELECT * from gmaps_countries";
$result = $db->query($sql) or die($db->error);
while ($obj = $result->fetch_object()) {
$obj->xml = simplexml_load_string($obj->geometry);
$obj->geometry = '';
foreach ($obj->xml->Polygon as $value) {
$obj->multi = 'multigeo';
}
$results[] = $obj;
}
echo json_encode($results);
当你需要时,只需调用jsonCountries()
。希望这能帮到你!
if ('multi' in country)
。 - MrUpsidown