NativeScript Google-Places API -->如何使用?



我正在开发使用Google-Maps-API的应用程序,我想添加search-box使用Google-Places-API的字段来自动完成到目前为止用户键入的内容 ,然后保存所选项目值。

https://github.com/jonny720/do-here-client

所以我得到了我的Google-Places-API密钥,我真的不知道把它放在哪里以及如何实现这个API。

Google-Maps-API被放置在androidMAnifest中.xml并且工作正常。

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/nativescript_google_maps_api_key"
/>

现在我在哪里实际实现Google-Places-API,我需要在哪里放置密钥?

谢谢!

PlacesMaps-utils插件已经可用。您可以使用它们。你试过吗?

好的,这就是我所做的,它奏效了:

我构建了一个PlacesService,这是代码:

autoCompleteUrl = 'https://maps.googleapis.com/maps/api/place/autocomplete/xml?input=';
autoCompleteUrl2 = '&key=API_KEY'

urlReq='https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=';
urlreq2 = '&inputtype=textquery&fields=formatted_address,name,geometry&key=API_KEY' 

newPlace: any;
constructor(private http :HttpClient){}
auto(typed):any{
if (typed){
console.log("got to func",typed)
return this.http.get(this.autoCompleteUrl+typed+this.autoCompleteUrl2);
} 
}

findPlace(place):any {
// return this.http.get(this.urlReq+place+this.urlreq2);
this.http.get(this.urlReq+place+this.urlreq2)
.toPromise().then(res => {
this.newPlace = JSON.stringify(res);
console.log("#########", this.newPlace); 
});
}

现在我得到一个看起来像这样的JSON对象:

[{"candidates":[{"formatted_address":"United States","geometry":{"location":{"lat":37.09024,"lng":-95.712891},"viewport":{"northeast":{"lat":49.38,"lng":-66.94},"southwest":{"lat":25.82,"lng":-124.39}}},"name":"United States"}],"debug_log":{"line":[]},"status":"OK"}, {"candidates":[{"formatted_address":"United States","geometry":{"location":{"lat":37.09024,"lng":-95.712891},"viewport":{"northeast":{"lat":49.38,"lng":-66.94},"southwest":{"lat":25.82,"lng":-124.39}}},"name":"United States"}],"debug_log":{"line":[]},"status":"OK"}]

但是我如何拆分这个对象? 我只想获取Namegeometry属性。

谢谢!

相关内容

  • 没有找到相关文章

最新更新