在google map api中为每个国家分配不同的颜色 [英] Assign different color to each country in the google map api

查看:1123
本文介绍了在google map api中为每个国家分配不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在Google地图中为每个国家分配不同的颜色吗?
我拥有存储每个国家相同颜色数据的数据。
例如:



在世界地图



蓝色覆盖到印度,然后是RED Afganishtan .. .etc
如果它需要使用多边形进行着色,那么让我知道每个国家知道每个国家边界的示例。
谢谢

解决方案

您可以轻松地通过将适当的GeoJSON加载到您的地图上(可以找到更多信息< href =https://developers.google.com/maps/documentation/javascript/datalayer =nofollow noreferrer> here )。
例如,在下面,我通过



注意:from 这里可以根据您的要求创建不同的GeoJSON文件,例如文件大小,覆盖区域和分辨率。

Does anyone know how to assign a different color to each country in a google map? I have data which stored each country with same colored data. e.g:

in the world map

Blue overlay to India, then RED Afganishtan...etc If its need to be color by using polygon then let me know any example with knowing border of each country lat long. Thanks

解决方案

You can easily do that by loading an appropriate GeoJSON onto your map (you can find more information here). For example, at the below, I created a medium resolution GeoJSON of the world via here and uploaded it to my server, and then by the following script, I was able to config the Google Map API to show countries in different colors.

                // Load GeoJSON.
            map.data.loadGeoJson('http://domain/geojson.json');

            map.data.setStyle(function(feature) {
                var color = 'green';
                if (feature.getProperty('wb_a2') === "AU") {
                    color = 'red';
                }
                if (feature.getProperty('wb_a2') === "IR") {
                    color = 'yellow';
                }
                return /** @type {google.maps.Data.StyleOptions} */({
                    fillColor: color,
                    strokeColor: color,
                    strokeWeight: 2
                });
            });

As you can see the default color is Green and both of Iran and Australia have got different colors.

Notice: from here you can create different GeoJSON files according to your requirements such as the file size, covering the area and the resolution.

这篇关于在google map api中为每个国家分配不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆