【问题标题】:How to create a Google Address Autocomplete search box with additional hidden fields (longitude, latitude)?如何创建带有附加隐藏字段(经度、纬度)的 Google 地址自动填充搜索框?
【发布时间】:2020-10-27 12:36:04
【问题描述】:

希望有人能提供帮助。

我目前正在构建一个网站,其中存储一些场所(位置)信息以供在应用程序中使用。

我想要实现的是,在用户提供两件事的模式窗口中输入:

  1. 场地名称(用户可以随意命名 - 这很简单)。
  2. 一个 Google 地址自动填充框,用户开始输入地址并自动填充结果 - 这是我需要帮助的部分。
  3. 整个地址需要是单个输入,因为我将其存储为一个大字符串(我不需要将其分解为街道、地址、邮政编码等)
  4. 我还需要从自动创建的地址中获取填充地址的经度和纬度,并将其应用于隐藏字段。

理想情况下,我希望通过从 Google 地址中选择一个选项来强制用户提交表单,而不是手动输入内容。但这并不是疯狂的必需品,更多的是锦上添花。

这是我的表单输入:

<form method="POST" action="{{ route('manage.venue.create', $url_extension) }}">
    @csrf

    <div class="form-group">
        <label for="amount">Venue Name</label>
            <input id="name"
                   type="text"
                   class="form-control"
                   name="name"
                   autofocus
                   placeholder="Enter Venue Name">
    </div>

    <div class="form-group">
        <label for="amount">Address</label>
        <input id="address"
               type="text"
               class="form-control"
               name="address"
               autofocus
               placeholder="Enter Location Name">
        <input type="hidden" name="latitude" id="latitude" value="0" />
        <input type="hidden" name="longitude" id="longitude" value="0" />
    </div>

    <br>

    <button type="submit" class="btn btn-primary">Save</button>

</form>

我知道所有的魔法都需要在 Javascript 中发生,我在网上找到了很多关于创建这些自动完成框的有用资源,但是它们都没有真正满足我的需要。此外,我看到的所有示例都因 Google API 的不同版本而异,有些已经很老了。而且他们都倾向于有一个大的谷歌地图,还有一个我不需要的下拉图钉。

我已经使用 API 密钥创建了一个 GCP 项目,并启用了所有必要的 API。

非常感谢您对此提供任何帮助,谢谢。

【问题讨论】:

  • 虽然你的说法正好相反,但看起来你在请求most basic implementation of the Places Autocomplete widget 之前没有做过任何研究。官方文档也充满了示例和有用的信息。
  • 感谢您的回复。我确实看到了这一点,并且在我最终实施时,它只是没有用。我什至添加了实现地图 API 的脚本标签。另外,我如何将 long/lat 作为表单请求的一部分添加?这似乎只是将其转储到浏览器窗口中
  • It didn't work 不是一个好的问题描述。我提供的链接只是一个简单的演示。您需要澄清您正在尝试做什么,并学习如何调试 javascript 以及使用 JS 进行 DOM 操作的基础知识。我提供的代码并没有只是将其转储到浏览器窗口中——它修改了页面上 DOM 元素的内部 HTML。您可以对表单元素或其他 HTML 元素执行相同的操作,但这对于此处的问题来说实在是太宽泛了
  • 我想我需要复习一下我的 JavaScript 知识。我希望有人已经实现了类似的东西并可以提供一些指导。无论如何感谢您的帮助。
  • 显然有人已经这样做了,是的。我提供的代码获取了该地点的纬度和经度(place.geometry.location.lat()place.geometry.location.lng()),并且您有一个包含 2 个输入(纬度和经度)和一个地址字段的表单。看看你能从getPlace()方法得到什么,那么你的问题是关于如何Set the value of an input field

标签: javascript html google-maps-api-3 google-places-api googleplacesautocomplete


【解决方案1】:

已解决:http://jsfiddle.net/upsidown/GVdK6/

function initialize() {

var ac = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')), {
  types: ['geocode']
});

ac.addListener('place_changed', function() {

var place = ac.getPlace();

if (!place.geometry) {
  // User entered the name of a Place that was not suggested and
  // pressed the Enter key, or the Place Details request failed.
  window.alert("No details available for input: '" + place.name + 
 "'");
  return;
}

var html = '<div>Latitude: ' + place.geometry.location.lat() + 
'</div>';
html += '<div>Longitude: ' + place.geometry.location.lng() + 
'</div>'; 

  document.getElementById('geometry').innerHTML = html;
  });
  }

initialize();

感谢您的帮助。

【讨论】:

  • 请不要添加“谢谢”作为答案。相反,您认为最有帮助的 accept the answer。 - From Review
  • 我试过了,但它说:“你可以在 2 天内接受你自己的答案”
  • 您不接受自己的答案,而是提示在评论中帮助您的人发布答案。
  • 添加一个帮助您解决问题的代码作为答案并在两天后接受它:)
猜你喜欢
  • 2018-07-15
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 2013-06-20
  • 2012-09-30
  • 1970-01-01
相关资源
最近更新 更多