3

I am using the Google Maps API and currently successfully adding markers based on exact locations, but I would like to also be able to add markers over countries without needing to have the coordinates of that country.

Is this possible?

Tom
  • 12,776
  • 48
  • 145
  • 240

2 Answers2

14

It is highly possible indeed. Use GeoCoder : https://developers.google.com/maps/documentation/javascript/geocoding

GeoCoder can return the lat / lngs for a country, just by knowing its name.

Assuming you already have a map :

geocoder = new google.maps.Geocoder();

function getCountry(country) {
    geocoder.geocode( { 'address': country }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
           map.setCenter(results[0].geometry.location);
           var marker = new google.maps.Marker({
               map: map,
               position: results[0].geometry.location
           });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

getCountry('USA');
getCountry('Brazil');
getCountry('Denmark');

Will place markers on your map in the direct center of USA, Brazil and Denmark.

enter image description here

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • Hi... I know I've accepted this already, but can you think of any way to eliminate the OVER_QUERY_LIMIT status problem? It is only allowing me to add 11. – Tom Sep 12 '13 at 08:16
  • Hi, I am in bit of a hurry this morning, but the see [this answer](http://stackoverflow.com/questions/7649155/avoid-geocode-limit-on-custom-google-map-with-multiple-markers/7651681#7651681), which I think is OK. Or see [this solution in PHP](http://www.webstutorial.com/google-server-side-geocoding-php-infobox/website-tweaks/google), if you use that. I actually, coincidally, have to make som GeoCoder myself later on today, possible including many countries (finds of fungi in countries without lat/lng - will update the answer later on (or tell if you get one of the suggestions to work) – davidkonrad Sep 12 '13 at 08:24
  • Hi. I found this which seems to do the job... http://stackoverflow.com/questions/9805529/geocoding-api-over-query-limit – Tom Sep 12 '13 at 09:02
  • Tnanks for info! This looks very good! Same approach as #1 mentioned above. I dont like the delays as a permanent "solution", so I will go further and store successfully results in a table, by ajax. Then first check the table - then lookup in GeoCoder. By that a database with lat/lngs will grow quickly and can be used among several projects. For me it is the same countries and regions anyway, 500 max, outside DK where lat/lng is missing, which is repeated throughout the databases. regards, – davidkonrad Sep 12 '13 at 09:31
0

According to the Maps API, marker.position is a required property, and it must be of LatLng type. So at the very least you'd either need to know the coordinates at the centre of the country, or an bounded array of coordinates for the country from which you you can extrapolate the coordinates for the centre.

I believe that if you use the Geocoder with just the country name, the service will provide you with the coordinates for the exact centre of the country. I've not tested this, and I don't know what if there's a limit to how much you can use it, but it would be worth you checking it out as a solution to your question.

For example:

var geo = new google.maps.Geocoder();
geo.geocoder.geocode('Spain', function (data) {
  // the coordinates are held in data[0].geometry.location.lat() and data[0].geometry.location.lng().
});
Andy
  • 61,948
  • 13
  • 68
  • 95