Javascript Google Maps v3 - 如何在初始化时使用地址居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6140303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:25:12  来源:igfitidea点击:

Google Maps v3 - How to center using an address on initialize?

javascriptgoogle-maps-api-3

提问by TerryMatula

Using Google Maps API v3, is there a way to set the center of the map on initialize? I have a workaround using this code:

使用 Google Maps API v3,有没有办法在初始化时设置地图的中心?我有一个使用此代码的解决方法:

var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    codeAddress('germany');
  }

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

The only problem is that when it initializes, it centers it to the "latlng" for a split second. I'm can't figure out how to set the center in "myOptions". I though I could return "results[0].geometry.location" from the codeAddress function and pass it to myOptions, but that doesn't work.

唯一的问题是,当它初始化时,它会在一瞬间将它集中到“latlng”。我不知道如何在“myOptions”中设置中心。我虽然可以从 codeAddress 函数返回“results[0].geometry.location”并将其传递给 myOptions,但这不起作用。

Thanks for any help.

谢谢你的帮助。

UpdateSince I can't remove "center" altogether, I'm wondering if there's a way to pass the address to the options.

更新由于我无法完全删除“中心”,我想知道是否有办法将地址传递给选项。

From Google API:

To initialize a Map, we first create a Map options object to contain map initialization variables. This object is not constructed; instead it is created as an object literal. There are two required options for every map: centerand zoom.

来自谷歌 API:

要初始化 Map,我们首先创建一个 Map 选项对象来包含地图初始化变量。这个对象不是构造的;相反,它被创建为对象字面量。每个地图都有两个必需的选项:centerzoom

回答by Kasper Vesth

Well a simple solution could be to initialize the map in your codeAddress function:

那么一个简单的解决方案可能是在您的 codeAddress 函数中初始化地图:

var geocoder, map;

function codeAddress(address) {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var myOptions = {
                zoom: 8,
                center: results[0].geometry.location,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        }
    });
}

This should solve the problem.

这应该可以解决问题。