【问题标题】:What script to include for both Google Places and GeocodingGoogle 地方信息和地理编码要包含的脚本
【发布时间】:2017-07-26 22:31:12
【问题描述】:

我正在努力让 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>

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

请有人告诉我要包含哪些脚本以使两者一起工作?老实说,在这一点上,我发现文档相当混乱。

【问题讨论】:

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


    【解决方案1】:

    它应该适用于您的第一个包含。它包含基本的 Google Maps API加上 Places 库。

    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);
        }
      });
    };
    &lt;script src="https://maps.googleapis.com/maps/api/js?libraries=places&amp;callback=initMap" async defer&gt;&lt;/script&gt;

    【讨论】:

    • 谢谢 Maarten,我已经收到了有关 Places 功能包含的回调,因此我已将您的包含修改为我认为适合我的内容:
    • 请耐心等待,我正在和 SO 编辑器做噩梦 :)
    • 地方搜索可以正常工作,但是当我到达地理编码行时(geocoder = new google.maps.Geocoder();)我得到'google is not defined',所以出于某种原因,包括似乎没有加载地理编码的东西
    • 确保只在调用回调后引用 google 命名空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多