要为Google地方信息和地理编码添加哪些脚本

时间:2017-07-26 22:31:13

标签: google-maps google-places-api google-geocoder

我正在努力让Google地方信息查找和地理编码在同一页面中协同工作。我认为我加载这两个API的方式存在冲突。

我正在使用这个来加载场所API

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=*****************&callback=initAutocomplete&types=address" async defer></script>

地方查找可以正常使用。

对于地理编码,这是我正在努力工作的第一行代码

geocoder = new google.maps.Geocoder();

如果我在我的页面中包含它,它可以工作(好吧,不会抛出错误):

<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

但这与我已经拥有的网址几乎相同,包括它会破坏Places查找。当我同时包含这两个脚本时,我也会收到警告,告诉我多次包含Maps API可能会导致问题。

请有人告诉我要包含哪些脚本以便两者一起工作?说实话,我发现这些文档很混乱。

1 个答案:

答案 0 :(得分:3)

它应该可以正常使用你的第一个包含。它包含基础Google Maps API plus Places库。

&#13;
&#13;
var initMap = function () {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    address: '10 Downing St, Westminster, London SW1A 2AA, UK',
  }, function (results, status) {
    if (status === 'OK') {
      var result = results[0];
      alert('latitude: ' + result.geometry.location.lat());
    }
    else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
};
&#13;
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap" async defer></script>
&#13;
&#13;
&#13;