【发布时间】:2020-11-14 02:40:32
【问题描述】:
试图从谷歌地图预测信息中获取经纬度。目前,我正在使用google-maps 组件。
我也设法在ngx-google-places-autocomplete 中加入了搜索栏。我需要的是在用户单击输入时获取第一个搜索结果项的坐标。
这是我目前所拥有的:
<input
id="over_map"
ngx-google-places-autocomplete
class="form-control location-search d-flex justify-content-center"
#placesRef="ngx-places"
[(ngModel)]="searchLocation"
(autocomplete)="valueCompleted($event)"
(keydown)="onKeydown($event,searchLocation)"
(onAddressChange)="handleAddressChange($event)"/>
<google-map
id="google_map"
height="330px"
width="100%"
[center]="center"
[zoom]="zoom"
[options]="options"
(mapClick)="clickLocation($event)"
>
</google-map>
在我的onKeydown 方法中:
onKeydown($event: KeyboardEvent, searchLocation) {
if ($event.key === 'Enter') {
console.log(JSON.stringify(this.map.getCenter()));
let searchPrediction: any;
const displaySuggestions = function(
predictions: google.maps.places.QueryAutocompletePrediction[],
status: google.maps.places.PlacesServiceStatus
) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
console.log('Current prediction', predictions[0]);
console.log('Location Details:', predictions[0].place_id);
searchPrediction = predictions[0];
};
const service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: this.searchLocation }, displaySuggestions);
}
}
这将获取所有预测项,并获取predictions[0] 看到的所有预测项中的第一个。据我所知,我只能从这里得到place id。
例如,mon 的搜索结果如下:
description: "Montreal, QC, Canada", id: "64f76606728b9536f3b6f03703a296affd47ca6c", matched_substrings: Array(1), place_id: "ChIJDbdkHFQayUwR7-8fITgxTmU", reference: "ChIJDbdkHFQayUwR7-8fITgxTmU", …}
【问题讨论】:
-
您似乎没有调用Places Details,其中将包含您需要的必要数据,例如
geometry,它将提供所选地点的纬度坐标。我建议你看看 Autocomplete Widget 以了解如何在 vanillaJS 中执行此操作:developers.google.com/maps/documentation/javascript/examples/…
标签: angular typescript google-maps google-maps-api-3