简体   繁体   English

Google商家信息自动填充功能 - 修改结果

[英]Google Places Autocomplete - modifying results

I have some standard google places autocomplete code that works fine. 我有一些标准的谷歌地方自动完成代码,工作正常。 I want to modify the result when the user selects an address from the list. 我想在用户从列表中选择地址时修改结果。

For example one of the results is 例如,其中一个结果是

'45 Alexandra Road Holiday Flats, Alexandra Road, Southport, United Kingdom' '45 Alexandra Road Holiday Flats,Alexandra Road,Southport,United Kingdom'

When the user selectes it from the list I would like the following value to appear in the text box 当用户从列表中选择它时,我希望在文本框中显示以下值

'45 Alexandra Road Holiday Flats, Alexandra Road, Southport, PR4 5JD' '45 Alexandra Road Holiday Flats,Alexandra Road,Southport,PR4 5JD'

ie, I want the post/zip code to be inserted and the United Kingdom to be removed. 即,我希望插入邮政编码和英国邮政编码。

I can do this in the place_changed event, the post/zip code is available and I can do some string manipulation and get the result I want. 我可以在place_changed事件中执行此操作,post / zip代码可用,我可以执行一些字符串操作并获得我想要的结果。

However when I assign it to the txtBox at the end of the 'place_changed' function, something then changes it back to the original selected value without the post/zip code. 但是当我在'place_changed'函数末尾将它分配给txtBox时,会将其更改为原始选定值而不使用post / zip代码。 This is something that has changed recently as my code used to work. 这是最近因我的代码工作而发生变化的事情。 The assignment/binding of the value to the txtBox happens after the place_changed event now it would seem. 将值赋值给txtBox发生在现在看起来像place_changed事件之后。

var autoCompleteObject = new google.maps.places.Autocomplete(txtBox[0], GooglePlacesOptions);
// Autocomplete Listener
google.maps.event.addListener(autoCompleteObject, 'place_changed', function () {

    var place = autoCompleteObject.getPlace();

    txtBox.val('45 Alexandra Road Holiday Flats, Alexandra Road, Southport, PR4 5JD');

});

Any ideas on how to achieve this would be greatly appreciated. 任何关于如何实现这一点的想法将不胜感激。

Many thanks, 非常感谢,

Mark 标记

I have decided not to use the AutoComplete functionality and use the PlacesService.textSearch() instead. 我决定不使用自动完成功能,而是使用PlacesService.textSearch()。

Quite easy and you have more control over results and how they are displayed etc.. 非常简单,您可以更好地控制结果以及它们的显示方式等。

So the question still remains but my head is less sore :) 所以这个问题仍然存在,但我的头脑不那么疼:)

作为参考,我发现以下答案作为一种有效的解决方法很有帮助: 只显示google.maps.event.addListener()中的街道地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM