2010-12-10 112 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); 
      } 
     });