怎么做自动完成谷歌的地方api

时间:2016-01-31 20:57:53

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

我有一个地方ID,使用我需要实施自动填充本地区域的Google地方ID。

我在谷歌搜索过,我找到了参考资料:

https://developers.google.com/maps/documentation/javascript/examples/place-details

但上述参考文献

document.getElementById('map');

由于我在屏幕上没有任何地图,它只是一个html表格。

我已完成城市自动填充功能:

    function initialize() {

    var input = document.getElementById('to-city');
    var options = {types: ['(cities)'],componentRestrictions: {country: 'in'}};

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

    autocomplete.addListener('place_changed', function() {

    var place = autocomplete.getPlace();

    if (!place.geometry) {
      return;
    }
    //alert(place.place_id);
    });
}        
google.maps.event.addDomListener(window, 'load', initialize);

以上代码对于使用placeid获取场所是否有用?

为了您的信息,我也有api代码。

提前感谢!

1 个答案:

答案 0 :(得分:1)

如果您有placeId,则不需要自动填充。从example in the documentation修改示例以删除地图。

代码段

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

function initMap() {
  var service = new google.maps.places.PlacesService(document.getElementById('map'));

  service.getDetails({
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
  }, function(place, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      document.getElementById('info').innerHTML = '<div><strong>' + place.name + '</strong><br>' +
        'Place ID: ' + place.place_id + '<br>' +
        place.formatted_address + '</div>';
    }
  });
}
google.maps.event.addDomListener(window, 'load', initMap);
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="info"></div>
<div id="map"></div>

相关问题