Javascript 显示地图并突出显示某些国家/地区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12696526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:03:24  来源:igfitidea点击:

Display a map and highlight certain countries

javascripthtmlgoogle-mapsmaps

提问by mdcarter

Here's what I'm struggling to do : Display a map on page (Google map or any of it competitors, free is possible...) and highlight certain countries on this map (the list of theses countries would be stored in a variable by exemple).

这是我正在努力做的事情:在页面上显示地图(谷歌地图或任何它的竞争对手,免费是可能的......)并在这张地图上突出显示某些国家(这些国家的列表将被存储在一个变量中例)。

I tried to draw polygons on the map with a geo-json list of (almost) all the countries but it's too heavy.

我试图用(几乎)所有国家的 geo-json 列表在地图上绘制多边形,但它太重了。

Any ideas ?

有任何想法吗 ?

回答by Jpsy

You can create very complex polygons in Maps without a drop in performance if you store them in KML file format and load them as KmlLayers. KML is an XML based vector file format for geo data. The beauty of using KML files in Google Maps is that they are rendered on Google servers into simple PNG files, matching the current resolution and bounding box of your Maps view at any time, no matter how complex the original polygon data is. So all complexity is handled server side at Google.

如果将它们存储为 KML 文件格式并将它们加载为KmlLayers ,则可以在地图中创建非常复杂的多边形而不会降低性能。KML 是一种基于 XML 的地理数据矢量文件格式。在 Google 地图中使用 KML 文件的美妙之处在于,它们在 Google 服务器上呈现为简单的 PNG 文件,无论原始多边形数据多么复杂,都可以随时匹配您地图视图的当前分辨率和边界框。所以所有的复杂性都在谷歌服务器端处理。

Here is a sample of a project, where I have used a KML mask for one of the federal states of Germany.

这是一个项目示例,我在其中使用了德国一个联邦州的 KML 掩码。

For a quick review KML files can also be loaded directly into the Google Maps public website (by entering their URL into the search field). Here is the KML file that I have used in the above project, loaded directly into Google Maps. (I have actually used a KMZ file here, but that is simply a KML file packed into a ZIP archive. You can pack and unpack it with normal ZIP tools.)[UPDATE: This does not work anymore in the new Maps version (started in 2014).]

为了快速查看,KML 文件也可以直接加载到 Google 地图公共网站(通过在搜索字段中输入其 URL)。这是我在上述项目中使用的 KML 文件,直接加载到 Google Maps 中。(我实际上在这里使用了一个 KMZ 文件,但这只是一个打包到 ZIP 存档中的 KML 文件。您可以使用普通的 ZIP 工具打包和解压它。)[更新:这在新的地图版本中不再适用(开始2014 年)]

Please be aware that Google implies a size limit to KML/KMZ files (currently 10 Mb uncompressed, 3 Mb compressed - details see here). If your KML files should be to large, there is software available to reduce the complexity (see examples hereand here).

请注意,Google 暗示了对 KML/KMZ 文件的大小限制(当前未压缩为 10 Mb,压缩为 3 Mb -详细信息请参见此处)。如果您的 KML 文件很大,可以使用软件来降低复杂性(请参阅此处此处的示例)。

Please notice that KML files are only a solution if your polygons are static (like the borders of countries or cities) and not generated on the fly (e.g. depending on any user input). For dynamic polygons you will still have to use the Polygon class of the Maps API with the complexity restrictions that you have mentioned. (But you are still free to use KML layers and switch them on and off depending on user input, as long as the polygon details don't have to change.)

请注意,如果您的多边形是静态的(例如国家或城市的边界)并且不是动态生成的(例如,取决于任何用户输入),则 KML 文件仅是一种解决方案。对于动态多边形,您仍然必须使用具有您提到的复杂性限制的 Maps API 的 Polygon 类。(但您仍然可以自由使用 KML 图层并根据用户输入打开和关闭它们,只要多边形细节不必更改。)

回答by bjornd

If you need just a boundaries of the countries with different colors you can try jVectorMap.

如果您只需要具有不同颜色的国家/地区的边界,您可以尝试jVectorMap