谷歌地方自动填充不建议一些地方

时间:2018-02-12 18:53:25

标签: angular google-maps google-maps-api-2

我正在构建一个angular-cli应用程序,用户可以通过键入选择他们想要的目的地,我的应用程序根据用户输入建议一些地方。为此,我正在使用@agm/core 基本上我的代码是功能性的,但问题是它不是暗示某些地方。当我们进行正常的谷歌地图搜索时会显示这些地方。

例如,当我搜索“Kelaniya大学”时,我的应用程序说“没有结果”,但另一方面this网站的谷歌动力自动完成,甚至建议大学里面的院系。

所以我需要弄清楚这是什么原因?是我的代码错误还是某种 Pro 版本的谷歌地图

这是我的代码

在app.module.ts中导入数组

imports: [
  BrowserModule,
  RoutesModule,
  MatFormFieldModule,
  MatNativeDateModule,
  MatInputModule,
  BrowserAnimationsModule,
  AgmCoreModule.forRoot({
    apiKey: 'my api key',
    libraries : ['places']
  })

],

app.component.ts

import {Component, ElementRef, NgZone, OnInit, ViewChild} from '@angular/core';
import { MapsAPILoader} from '@agm/core';
import {} from '@types/googlemaps';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  title = 'app';

  @ViewChild('search') public searchElement: ElementRef;
  constructor(private  mapsAPILoader: MapsAPILoader, private ngZone: NgZone) {
  }
  ngOnInit() {
    this.mapsAPILoader.load().then(
      () => {
        let autocomplete = new google.maps.places.Autocomplete(this.searchElement.nativeElement,{ types:['address']});
        autocomplete.setComponentRestrictions({'country' : ['lk']});
        autocomplete.addListener('place_changed' , () => {
          this.ngZone.run( () => {
            let place: google.maps.places.PlaceResult = autocomplete.getPlace();
            if ( place.geometry === undefined || place.geometry == null ) {
              return;
            }
          })
        })
      }
    )
  }

}

app.component.html

<div class="container">
  <h1>Google maps</h1>
<div class="form-group">
  <input type="text" autocomplete="off" placeholder="search for lacaito" autocapitalize="off" class="form-group" #search>
</div>

</div>

1 个答案:

答案 0 :(得分:2)

您可以使用以下格式初始化地点自动填充

let autocomplete = new google.maps.places.Autocomplete(this.searchElement.nativeElement,{ types:['address']});

请注意代码中的类型限制,此限制指示仅返回类型地址的结果。

现在看一下地理编码工具中的'Kelaniya大学':

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#place_id%3DChIJR8eAMwNY4joRM96rLIkHxxI

您可以轻松看到此功能的类型为establishment, point_of_interest, university。它没有address类型,因此它不会显示在您的自动填充中。

enter image description here

我希望我的回答能解决你的疑问!

相关问题