Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I show a single country (masked out) in GoogleMaps?

Tags:

google-maps

Is it possible to mask away neighbor countries in GoogleMap? In my case I just want to show Sweden on one view. On a second view just Norway, Denmark etc etc...

I know it is possible to draw polygon lines and fill them accordingly on to Maps API. Problem is that or the result sets get huge or the lines get very rough etc... Would be nice if GoogleMaps could deliver a country at the time without neighbors.

Does anyone know about a good coordinate database with country borders?

like image 994
NIclas Lindbegr Avatar asked May 11 '10 13:05

NIclas Lindbegr


People also ask

How do I show only one country on Google Maps?

we can allow only specific country to autocomplete search in google maps. We are using google map autocomplete for choosing address with latitude and longitude. but if you require to search only one country like in, us, uk etc then you can do it using setComponentRestrictions option in google map.

How do I highlight country borders on Google Maps?

To achieve this, you will basically need to look for the coordinates of the state or country on GADM. Once you have them, draw a shape on the Google Maps with the Polygon object providing an array with all the coordinates that play basically as borders of the place that you want to highlight.

Why are some countries not highlighted on Google Maps?

Many nations across the globe have disputed international borders due to past or current conflicts with other nations. To avoid any controversy, and as an indirect sign that a country has conflicted borders, Google doesn't highlight it when you search its name.

Which country does not allow Google Maps?

That's why Google products, including Maps are blocked in China. It is worth mentioning that another reason why Google Maps is not available in certain regions is that those territories are still subject to conflict or are not internationally recognized.


1 Answers

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.

Here is a sample of a project, where I have used such a masking for one of the federal states of Germany. (The surroundings in this case are not fully masked away but only dimmed. But you can also mask them off just as easily by changing the alpha value of the fill color in the KML file.)

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.)
BEGIN UPDATE 07/2015: Unfortunately Google has REMOVED the possibility to directly enter the URL of a KML/KMZ file into the search field of their public Google Maps when they launched its redesigned version in 2014. You can still use all techniques discussed in this answer but you cannot simply preview KML files by entering their URL in the search field anymore. If you need the KMZ mentioned above for further analysis, you can download it here. END UPDATE

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 here and here).

You will find free map data at many places on the Internet, most of them being in ESRI shapefile format. A great source for country maps is http://www.gadm.org/ with the big advantage of providing their data in KML/KMZ format too. If you should have to convert ESRI shapefiles to KML, have a look at the great (and free) OGR converter.

Please notice that KML files are only a solution if your polygons are static (like the border of Sweden is) and not 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.

like image 54
Jpsy Avatar answered Oct 18 '22 16:10

Jpsy