Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a google map around a country if you just have the country name

I have a page about hiking in various countries. I am trying to make a page like that for each country, but not sure how to automatically center the google map around a country and also have it adjust the focus depending on the size of the country.

Here are my problem pages:

http://www.comehike.com/outdoors/country.php?country_id=6&country_name=Belarus

http://www.comehike.com/outdoors/country.php?country_id=1&country_name=United%20States

You see how the centering and focus size is static? That is because I currently use this code to center the map:

function initialize( )
{
    var myLatlng = new google.maps.LatLng(37.775, -122.4183333);

    var myOptions =
    {
      zoom: 2,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

But that is only because I am not sure how to do it by country name, and adjust the size automatically. How can I do this all by passing a country name into the initialize function?

Thanks!

like image 703
Genadinik Avatar asked May 22 '11 05:05

Genadinik


People also ask

How do I center a Google map?

Please go to your map and drag and drop your map to the position you wish. You can also zoom in your map a little bit with mouse wheel or zoom cotrols on the bottom right corner of the map. Please remember to save your map after any changes. Hope that helps.

How do I create a custom location map?

Start by heading to maps.google.com. Click on the menu icon on the top left hand side of the screen and select “Your Places.” (The menu icon is just to the left of the search bar on the top left hand side of your screen.) Select the maps tab. Navigate to the very bottom of that window and select “Create a Map.”

Is there a way to display a single country in Google Map?

You can customize your map for a specific country or region in the following ways: Change the default language settings. Specify a region code, which alters the map's behavior based on a given country or territory.


1 Answers

You can transform country name to the coordinates using Geocoding service. Example provided by Google centers a map but it does not zoom it. To zoom it you should use map.fitBounds() method using LatLngBounds object returned by the geocoder. This is an example code:

var myLatlng = new google.maps.LatLng(37.775, -122.4183333);

var myOptions =
{
    zoom: 2,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var address = "Belarus";
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        map.fitBounds(results[0].geometry.bounds);
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});
like image 113
Yuri Stuken Avatar answered Apr 29 '23 04:04

Yuri Stuken