Google位置自动无法在我的php页面中运行

时间:2017-10-13 05:54:07

标签: php forms api google-api

我正在使用谷歌自动位置api,但它没有在页面上显示..你们可以弄清楚代码中有什么不对。

我的脚本是:

<script>
        function init() {
            var input = document.getElementById('locationTextField');
            var input2 = document.getElementById('locationTextField2');

             var autocomplete = new google.maps.places.Autocomplete(input);
             var autocomplete = new google.maps.places.Autocomplete(input2);

        }

        google.maps.event.addDomListener(window, 'load', init);
    </script>

我的HTML代码是这样的:

     <input type="text" id="locationTextField" placeholder="Source Location"/>
       <input type="text" id="locationTextField2" placeholder="Destination Location"/>

1 个答案:

答案 0 :(得分:0)

请在您的网站中使用有效的API密钥加载您的脚本

<script src="https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>&v=3.exp&sensor=false&libraries=places"></script>

以下是示例(仅限演示的api键):

&#13;
&#13;
function initialize() {
  new google.maps.places.Autocomplete((document.getElementById('source')), {
    types: ['geocode']
  });
   new google.maps.places.Autocomplete((document.getElementById('dest')), {
    types: ['geocode']
  });
  
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&dummy=.js"></script>

<input id="source" name="source" value="" type="text" size="50" placeholder="Source" />
<input id="dest" name="dest" value="" type="text" size="50" placeholder="Destincation" />
&#13;
&#13;
&#13;