2010-12-10 68 views

回答

43

您可以使用地理編碼查找國家的緯度/經度。
查看Google的這個sample

基本上你需要做這樣的事情:

var country = "Germany"; 
var geocoder; 

geocoder.geocode({'address' : country}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
    } 
}); 

在你的初始化函數,你需要設置地理編碼對象的值,就像這樣:

geocoder = new google.maps.Geocoder(); 

你需要制定適當的縮放級別,然後在將地圖居中後進行設置。

+0

我認爲這是重新定位地圖的最清潔和最簡單的方法...謝謝:) – 2014-02-12 05:02:55

0

我知道如何做到這一點的唯一方法是通過列出各個國家和他們各自的經緯度,以及已知的信息,您可以使用下面的代碼。

var myLatlng = new google.maps.LatLng(-34.397, 150.644); 
var myOptions = { 
    zoom: 8, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 

var map = new google.maps.Map(document.getElementById("map_canvas"), 
    myOptions); 

出於好奇,你會如何在v2中做到這一點?

+0

,你可以通國名的API提供的地理編碼器,並使用所得到的緯度/經度,就像你做了那裏。 – RYFN 2010-12-10 11:59:44

1

此代碼爲我工作:

 let geocoder = new google.maps.Geocoder(); 
     let location = "England"; 
     geocoder.geocode({ 'address': location }, function(results, status){ 
      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 
      } else { 
       alert("Could not find location: " + location); 
      } 
     });