【问题标题】:how to do autocomplete google places api如何自动完成谷歌地方api
【发布时间】:2016-01-31 20:57:52
【问题描述】:

我有一个地点 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 代码。

提前致谢!

【问题讨论】:

  • 你“拥有”的 placeId 是什么?要获取地点详细信息,请参阅the documentation

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


【解决方案1】:

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

代码 sn-p:

// 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>

【讨论】:

  • 好的,那么我的问题可能是错误的。我需要使用地点 ID 或其他任何方式获取城市中的本地区域。
猜你喜欢
  • 2014-01-02
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多