javascript - Google Maps API JS - 使用两个组合字段放置自动完成

标签 javascript google-maps google-maps-api-3

我来找你是因为我不能像我想的那样使用自动完成位置功能而不是 Google maps JS api v3。

确实,我想通过添加另一个字段的值来实现自动完成功能。

具体来说,我想要两个字段,一个是公司名称,另一个是地址。我希望仅在地址字段上执行自动完成,但要考虑公司名称。

因此,如果我在公司字段中填写“Company XYZ”并在地址中填写“New York, USA”,它应该查找“Company XYZ, New York, USA”。

一个输入的经典例子:

函数JS:

function initialize() {
        // Create the autocomplete object, restricting the search
        // to geographical location types.
        autocomplete = new google.maps.places.Autocomplete(
                /** @type {HTMLInputElement} */(document.getElementById('addr')),
                { types: ['establishment', 'geocode'] });
        // When the user selects an address from the dropdown,
        // populate the address fields in the form.
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            fillInAddress();
        });
    }

HTML 表单:

<input class="field" id="company_name" />
<input class="field" id="addr" />

您认为有可能吗?如果是,怎么办?

谢谢。

最佳答案

您可以使用自动完成服务并获得对两个字段值的查询预测。

参见 https://developers.google.com/maps/documentation/javascript/examples/places-queryprediction

var autocompleteService = new google.maps.places.AutocompleteService();

getPlacePredictions(document.getElementById('company').value + ', ' + document.getElementById('address').value);

function getPlacePredictions(search) {

    autocompleteService.getPlacePredictions({
        input: search,
        types: ['establishment', 'geocode']
    }, callback);
}

下面是自定义自动完成预测列表的完整示例(样式基于 Google 实现),具有搜索突出显示、地点详细信息请求和在 map 上使用标记显示。

JSFiddle demo

关于javascript - Google Maps API JS - 使用两个组合字段放置自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28566699/

相关文章:

javascript - 将自己传递给 OnClick 事件 JavaScript

jquery - 使用 Google map 标记绑定(bind) jQuery 元素点击

java - 从 Eclipse 的模拟器控制返回不准确的值

php - 隐藏和显示自定义标记在我的谷歌地图中不起作用

javascript - 谷歌地图 API v3 : Markers don't always continuously bounce

javascript - 使用 node.js/electron/javascript 在网页中运行异步命令

Javascript 显示输出计算

javascript - for 循环内的条件未通过。为什么?

android - 谷歌地图签名 APK Android

php - 如何将php结果传递给谷歌地图