【问题标题】:Center on a country by name in Google Maps API v3在 Google Maps API v3 中以国家名称为中心
【发布时间】:2010-12-10 11:34:03
【问题描述】:

有人能告诉我如何在 Google Maps API v3 上以国家名称为中心吗?我知道如何在 v2 中执行此操作,但需要在 v3 中执行。

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    您可以使用地理编码来查找国家/地区的纬度/经度。看看这个来自 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();
    

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

    【讨论】:

    • 我认为这是重新定位地图的最干净和最简单的方法...谢谢:)
    • @kzhen 这行得通,但在澳大利亚等某些国家/地区并没有放大那么多。有没有办法改变这个?
    【解决方案2】:

    这段代码对我有用:

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

    【讨论】:

      【解决方案3】:

      我知道如何做到这一点的唯一方法是列出国家/地区及其各自的经纬度和经度,知道这些信息后,您可以使用以下代码。

      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 中做到这一点?

      【讨论】:

      • 您可以将国家/地区名称传递给 API 中提供的地理编码器,并使用生成的纬度/经度,就像您在那里所做的那样。
      猜你喜欢
      • 2015-04-25
      • 2017-08-04
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2013-04-11
      • 2012-01-25
      • 2016-07-20
      • 1970-01-01
      相关资源
      最近更新 更多