Google 地方自动完成不建议某些地方
Google place autocomplete not suggesting some places
我正在构建一个 angular-cli 应用程序,用户可以通过键入 select 他们想要的目的地,我的应用程序会根据用户输入建议一些地方。为此,我使用 @agm/core
.
基本上我的代码是可用的,但问题是 它没有建议某些特定的地方。当我们进行正常的 google 地图搜索时,会显示这些地点。
例如,当我搜索“University of Kelaniya”时,我的应用程序说“没有结果”,但另一方面 this 网站的 google 支持的自动完成功能甚至暗示里面的院系大学。
所以我需要弄清楚这是什么原因?是我的代码 错误 还是任何 种 Pro 版本的 google map?
这是我的代码
导入数组 In 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>
您将地点自动完成初始化为以下形式
let autocomplete = new google.maps.places.Autocomplete(this.searchElement.nativeElement,{ types:['address']});
请注意代码中的类型限制,此限制指示 return 仅地址类型的结果。
现在看看地理编码器工具中的 'University of Kelaniya':
你可以很容易地看出这个特征的类型是establishment, point_of_interest, university
。它没有类型 address
,因此不会出现在您的自动完成中。
希望我的回答能解决您的疑惑!
我正在构建一个 angular-cli 应用程序,用户可以通过键入 select 他们想要的目的地,我的应用程序会根据用户输入建议一些地方。为此,我使用 @agm/core
.
基本上我的代码是可用的,但问题是 它没有建议某些特定的地方。当我们进行正常的 google 地图搜索时,会显示这些地点。
例如,当我搜索“University of Kelaniya”时,我的应用程序说“没有结果”,但另一方面 this 网站的 google 支持的自动完成功能甚至暗示里面的院系大学。
所以我需要弄清楚这是什么原因?是我的代码 错误 还是任何 种 Pro 版本的 google map?
这是我的代码
导入数组 In 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>
您将地点自动完成初始化为以下形式
let autocomplete = new google.maps.places.Autocomplete(this.searchElement.nativeElement,{ types:['address']});
请注意代码中的类型限制,此限制指示 return 仅地址类型的结果。
现在看看地理编码器工具中的 'University of Kelaniya':
你可以很容易地看出这个特征的类型是establishment, point_of_interest, university
。它没有类型 address
,因此不会出现在您的自动完成中。
希望我的回答能解决您的疑惑!