Is there anyway so I can get boundries of countries in polygon coordinates value using Google map API.
3 Answers
See this similar question: Google Maps V3: Draw German State Polygons?
The Natural Earth Data Set in FusionTables has country polygons available in it. You can style them as you like.
I used it to create this example: http://www.geocodezip.com/v3_FusionTablesLayer_worldmap_linkto.html
Here is an example that uses a KmlLayer: http://www.geocodezip.com/v3_GoogleEx_layer-kml_world_countries_simple.html
code snippet:
var kmlLayer = null;
var map = null;
function openIW(layerEvt) {
if (layerEvt.row) {
var content = layerEvt.row['admin'].value;
} else if (layerEvt.featureData) {
var content = layerEvt.featureData.name;
}
document.getElementById('info').innerHTML = "you clicked on:<br>" + content;
}
function initialize() {
var chicago = new google.maps.LatLng(36.4278, -15.9);
var myOptions = {
zoom: 0,
center: chicago,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, "click", function() {
document.getElementById('info').innerHTML = "";
});
kmlLayer = new google.maps.KmlLayer('http://www.geocodezip.com/geoxml3_test/world_countries_kml.xml', {
preserveViewport: true,
suppressInfoWindows: true
});
kmlLayer.setMap(map);
google.maps.event.addListener(kmlLayer, 'click', openIW);
}
google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<table>
<tr>
<td>
<div id="map_canvas" style="height:200px; width:300px;"></div>
</td>
<td>
<div id="info"></div>
</td>
</tr>
</table>

- 158,664
- 13
- 220
- 245
-
Can your example be extended on two countries? – Léo Léopold Hertz 준영 Jul 30 '17 at 14:28
-
Sure. Not sure which example you are asking about, but that sounds like another question – geocodezip Jul 30 '17 at 18:24
-
Yes, it is. I am thinking if it is possible to visualise two countries by your code base and/or more countries. – Léo Léopold Hertz 준영 Jul 30 '17 at 18:39
-
1[multiple country example](http://www.geocodezip.com/v3_FusionTables_MultiCountryBrowser.html?countries=Afghanistan,Albania,Algeria,United%20States%20of%20America,Brazil) – geocodezip Jul 31 '17 at 09:46
-
Do you have the code for preview? I would like to give a list for the drawing. – Léo Léopold Hertz 준영 Jul 31 '17 at 12:45
-
1you mean `view-source`? I don't understand what you mean by "I would like to give a list for the drawing" (but again, this is not a subject for comments, it is a new question). – geocodezip Jul 31 '17 at 13:07
If you want more control over the displayed boundaries, I suggest using google.maps.Data
layer instead of Fusion Tables. Fusion tables and Fusion Layer has their limitations, for example on the World Map in geocodezip's example you can see, that if you zoom out the map, the boundaries will collapse to points. With Data Layer you can style specific boundaries, or hide them. Fusion Layer
allows only conditional style changes. You can also add your own click
, mouseover
, mouseout
listeners, you can change the boundary layer on the fly and more. Of course sometimes Fusion Layer
is better alternative, for example if you have data contained in Fusion Tables, but if you want more control Data Layer
is more than worthy alternative.
In my next working example I show World Country and US States boundaries rendered using Data Layer
as example. You can of course use your own boundary files. If you don't have GeoJson version available (only Shapefiles or KML files), you can convert them to GeoJson using utilities like mapshaper (simply run: mapshaper -i myshp.shp -o format=geojson out.geo.json
)
var map = null;
var my_boundaries = {};
var data_layer;
var info_window;
//initialize map on document ready
$(document).ready(function(){
var latlng = new google.maps.LatLng(40.723080, -73.984340); //you can use any location as center on map startup
var myOptions = {
zoom: 2,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(){
if(info_window){
info_window.setMap(null);
info_window = null;
}
});
$('#boundary_load_select').change(function(){
if($(this).val()==1){
loadBoundariesFromGeoJson("https://raw.githubusercontent.com/matej-pavla/Google-Maps-Examples/master/BoundariesExample/geojsons/world.countries.geo.json");
}else{
loadBoundariesFromGeoJson("https://raw.githubusercontent.com/matej-pavla/Google-Maps-Examples/master/BoundariesExample/geojsons/us.states.geo.json");
}
});
loadBoundariesFromGeoJson("https://raw.githubusercontent.com/matej-pavla/Google-Maps-Examples/master/BoundariesExample/geojsons/world.countries.geo.json");
});
function initializeDataLayer(){
if(data_layer){
data_layer.forEach(function(feature) {
data_layer.remove(feature);
});
data_layer = null;
}
data_layer = new google.maps.Data({map: map}); //initialize data layer which contains the boundaries. It's possible to have multiple data layers on one map
data_layer.setStyle({ //using set style we can set styles for all boundaries at once
fillColor: 'white',
strokeWeight: 1,
fillOpacity: 0.1
});
data_layer.addListener('click', function(e) { //we can listen for a boundary click and identify boundary based on e.feature.getProperty('boundary_id'); we set when adding boundary to data layer
var boundary_id = e.feature.getProperty('boundary_id');
var boundary_name = "NOT SET";
if(boundary_id && my_boundaries[boundary_id] && my_boundaries[boundary_id].name) boundary_name = my_boundaries[boundary_id].name;
if(info_window){
info_window.setMap(null);
info_window = null;
}
info_window = new google.maps.InfoWindow({
content: '<div>You have clicked a boundary: <span style="color:red;">' + boundary_name + '</span></div>',
size: new google.maps.Size(150,50),
position: e.latLng, map: map
});
});
data_layer.addListener('mouseover', function(e) {
data_layer.overrideStyle(e.feature, {
strokeWeight: 3,
strokeColor: '#ff0000'
});
});
data_layer.addListener('mouseout', function(e) {
data_layer.overrideStyle(e.feature, {
strokeWeight: 1,
strokeColor: ''
});
});
}
function loadBoundariesFromGeoJson(geo_json_url){
initializeDataLayer();
$.getJSON(geo_json_url, function(data){
if(data.type == "FeatureCollection"){ //we have a collection of boundaries in geojson format
if(data.features){
for(var i = 0; i < data.features.length; i++){
var boundary_id = i + 1;
var new_boundary = {};
if(!data.features[i].properties) data.features[i].properties = {};
data.features[i].properties.boundary_id = boundary_id; //we will use this id to identify boundary later when clicking on it
data_layer.addGeoJson(data.features[i], {idPropertyName: 'boundary_id'});
new_boundary.feature = data_layer.getFeatureById(boundary_id);
if(data.features[i].properties.name) new_boundary.name = data.features[i].properties.name;
if(data.features[i].properties.NAME) new_boundary.name = data.features[i].properties.NAME;
my_boundaries[boundary_id] = new_boundary;
}
}
if(my_boundaries[24]){ //just an example, that you can change styles of individual boundary
data_layer.overrideStyle(my_boundaries[24].feature, {
fillColor: '#0000FF',
fillOpacity: 0.9
});
}
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("maps", "3",{other_params:"sensor=false"});
</script>
<body style="margin:0px; padding:0px;" >
<select id="boundary_load_select">
<option value="1">Load World Boundaries</option>
<option value="2">Load US States Boundaries</option>
</select>
<div id="map_canvas" style="height:400px; width:500px;"></div>
</body>

- 5,303
- 1
- 28
- 44
-
Hey thank you dude i was searching for the url but your inner link worked for me thank you. Guys let me know if you want any help regarding plotting the country boundaries. – ashokdy Sep 08 '16 at 14:26
-
-
There is no uniform answer to that. For some countries you might not even be able to get them. For US, Australia and I also think NZ and CA you can find Census data where the boundaries are usually available. UK for example has Ordnance Survey. For some countries I couldn't find them at all for some they were paid. So you would have to google it. – Matej P. Dec 22 '16 at 12:10
-
-
1You start the same as country boundaries, but you have to find data for zip boundaries and that depends on country: for US you can find them (https://www.census.gov/cgi-bin/geo/shapefiles/index.php), for AU too, for some countries they are paid, for some I was unable to find any at all... So you have to search, there is no one place to get them all.. The problem though with zips is, that there's usually too many polygons for browser to handle. On tips how to do that check e.g. this answer: http://stackoverflow.com/a/37167093/2661226 – Matej P. May 08 '17 at 08:47
-
How did you get the country and state boundaries? - - Here a case where I would like find JSONs for these countries such that I could visualize the variables https://webapps.stackexchange.com/q/108181/66725 – Léo Léopold Hertz 준영 Jul 30 '17 at 10:30
I guess by getting boundaries, you want to draw polygon on their borders. For that you must know the coordinates that will help to draw the polygon. I have did this thing only for US. I have all coordinates of all states. And I draw polygon on every state. That makes US highlighted.
You can see my this question for US solution. But for other countries, you must need to know the coordinates.
-
I want to draw polygon for Dublin but don't know coordinates, in that case how can I draw polygons? – Shital Jachak Dec 22 '20 at 11:42