Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以谷歌地图API v3中的国家名称为中心_Javascript_Google Maps - Fatal编程技术网

Javascript 以谷歌地图API v3中的国家名称为中心

Javascript 以谷歌地图API v3中的国家名称为中心,javascript,google-maps,Javascript,Google Maps,有人能告诉我如何在谷歌地图API v3上以国家的名字为中心吗?我知道如何在v2中执行,但需要在v3中执行。我知道如何执行的唯一方法是列出国家及其各自的Lat和Lng,已知这些信息后,您可以使用以下代码 var myLatlng = new google.maps.LatLng(-34.397, 150.644); var myOptions = { zoom: 8, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP

有人能告诉我如何在谷歌地图API v3上以国家的名字为中心吗?我知道如何在v2中执行,但需要在v3中执行。

我知道如何执行的唯一方法是列出国家及其各自的Lat和Lng,已知这些信息后,您可以使用以下代码

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中执行此操作?

您可以使用地理编码查找该国的Lat/Lng。看看谷歌的这个

基本上你需要这样做:

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();
在初始化函数中,需要设置geocoder对象的值,如下所示:

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();

您需要确定适当的缩放级别,然后在地图居中后设置该级别。

此代码适用于我:

      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);
          }
      });

您可以将国家名称传递给API中提供的地理编码器,并使用生成的lat/lon,就像您在那里所做的那样。我认为这是重新定位地图的最干净、最简单的方法。。。谢谢:)@kzhen这是可行的,但对一些国家,如澳大利亚,它并没有放大太多。有没有办法改变这一点?