【问题标题】:Can't find any examples of google.maps.places.PlacesService() and getting errors找不到 google.maps.places.PlacesService() 的任何示例并出现错误
【发布时间】:2019-07-20 19:15:07
【问题描述】:

找不到任何 google.maps.places.PlacesServices() 的示例,它从具有组件限制的输入字段进行搜索,就像自动完成一样。 Google site只有参考资料,没有例子

我在下面有这个但收到一个错误提示

TypeError: 无法读取未定义的属性“innerHTML”

TypeError: 无法读取属性 'findPlaceFromQuery' of null

findLocationFromPlaces() {
    this.placesService = new google.maps.places.PlacesService(this.cityInput);

    const request = {
      query: 'San Francisco',
      fields: ['photos', 'formatted_address', 'name', 'rating', 'opening_hours', 'geometry']
    };

    this.placesService.findPlaceFromQuery(request
      // {
      //   types: ['(cities)']
      //   // componentRestrictions: { country: this.countrySelected },
      //   // types: ['geocode']  // 'establishment' / 'address' / 'geocode'
      // }
    ,
    (results, status) => {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        
      }
      else {
          
      }
    }
    );
  }

我的“this.cityInput”是

@ViewChild('city') cityInput: HTMLDivElement;
<input appGooglePlaces id="city" (onSelect)="setAddress($event)" (keydown.Tab)="onTabAway($event)" (focusout)="focusOutFunction($event)" (input)="onLocationChange($event)" [ngClass]="{'is-invalid': registerForm.get('city').errors && registerForm.get('city').touched}"
  formControlName="city" class="form-control google-place-input" [placeholder]="placeholder">

【问题讨论】:

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


    【解决方案1】:

    地点服务用于地点搜索。它不能像自动完成服务那样工作。此外,您不能将 componentRestrictions 传递给 findPlaceFromQuery()。此方法仅采用 3 个参数:查询、字段和可选的 locationBias(偏向但不限制搜索区域)。

    可以在 herehere's a working example 找到“从查询中查找位置”的文档。

    如果您还希望实现自动完成功能,例如将结果限制在特定国家/地区,请使用实际的Autocomplete service

    编辑无地图选项:

    1. 从查询中查找地点

      function findLocationFromPlaces() {
          let placesService = new google.maps.places.PlacesService(document.getElementById("map")); // i.e. <div id="map"></div>
      
          const request = {
              query: 'San Francisco',
              fields: ['photos', 'formatted_address', 'name', 'rating', 'opening_hours', 'geometry']
          };
      
          placesService.findPlaceFromQuery(request, function(results, status) {
              if (status === google.maps.places.PlacesServiceStatus.OK) {
                  console.log(results[0].formatted_address) // San Francisco, CA, USA
              }
              else {
                  console.log("doesn't work");
              }
          });
      }
      
    2. 地址形式的自动填充(完整示例here)+ 国家/地区限制

      function initAutocomplete() {
        // Create the autocomplete object, restricting the search predictions to
        // geographical location types.
        autocomplete = new google.maps.places.Autocomplete(
            document.getElementById('autocomplete'), {types: ['geocode'], componentRestrictions: {country: 'us'}});
      
        // Avoid paying for data that you don't need by restricting the set of
        // place fields that are returned to just the address components.
        autocomplete.setFields(['address_component']);
      
        // When the user selects an address from the drop-down, populate the
        // address fields in the form.
        autocomplete.addListener('place_changed', fillInAddress);
      }
      
      function fillInAddress() {
        // Get the place details from the autocomplete object.
        var place = autocomplete.getPlace();
      
        for (var component in componentForm) {
          document.getElementById(component).value = '';
          document.getElementById(component).disabled = false;
        }
      
        // Get each component of the address from the place details,
        // and then fill-in the corresponding field on the form.
        for (var i = 0; i < place.address_components.length; i++) {
          var addressType = place.address_components[i].types[0];
          if (componentForm[addressType]) {
            var val = place.address_components[i][componentForm[addressType]];
            document.getElementById(addressType).value = val;
          }
        }
      }
      
    3. Find Place 网络服务(文档here

      https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=San%20Francisco&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&key=YOUR_API_KEY

    4. 自动完成网络服务(文档here)+ 国家/地区限制

      https://maps.googleapis.com/maps/api/place/autocomplete/json?input=San+Francisco&types=geocode&components=country:us&key=YOUR_API_KEY

    希望这能澄清你的问题!

    【讨论】:

    • 在这种情况下,您实际上可以只使用 div,或在表单中实现自动完成,或使用 Web 服务等。有许多无地图选项(给出适当的属性)。请澄清您的用例,我会相应地修改我的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 2021-03-05
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多