google-maps - 将自动完成结果限制在 place_id 范围内
问题描述
我有两个文本框,它们都是自动完成的。当我改变它时,我们得到第一个place_id。第二个文本框应该在第一个的 place_id 内建议结果。
这是我的代码:
const options = {
types: ['(regions)'],
componentRestrictions: {country: 'gb'},
};
const search = new google.maps.places.Autocomplete( $("#address_1")[0], options);
google.maps.event.addListener(search, 'place_changed', function () {
var place = search.getPlace();
const options_b = {
types: ['(regions)'],
componentRestrictions: {country: 'gb'},
};
var options_2 = {
types: ['address'],
strictbounds: true,
radius:500,
//other parameters here..perhaps place_id ?
};
const search = new google.maps.places.Autocomplete( $("#address_2")[0], options_b);
});
所以假设我在第一个文本框中输入了 London,那么第二个文本框应该只建议伦敦内的地点。
解决方案
如果要将 an 的结果限制在Autocomplete
某个地方的范围内,请使用该地方的范围来限制Autocomplete
并设置strictBounds: true
选项(注意大写)
const search1 = new google.maps.places.Autocomplete($("#address_1")[0], options);
google.maps.event.addListener(search1, 'place_changed', function() {
var place = search1.getPlace();
var bounds = place.geometry.viewport; // bounds of place returned by the first autocomplete
var options_2 = {
types: ['address'],
strictBounds: true,
bounds: bounds
};
const search2 = new google.maps.places.Autocomplete($("#address_2")[0], options_2);
});
"use strict";
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCPJpjD-qcR_yIxJnS8maR5W9KB0E3EzYI&libraries=places">
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'))
const options = {
types: ['(regions)'],
componentRestrictions: {
country: 'gb'
},
};
const search1 = new google.maps.places.Autocomplete($("#address_1")[0], options);
google.maps.event.addListener(search1, 'place_changed', function() {
var place = search1.getPlace();
var bounds = place.geometry.viewport;
var marker = new google.maps.Marker({
position: place.geometry.location,
map: map
});
var rectangle = new google.maps.Rectangle({
bounds: bounds,
map: map
})
map.fitBounds(bounds);
var options_2 = {
types: ['address'],
strictBounds: true,
bounds: bounds
//other parameters here..perhaps place_id ?
};
const search2 = new google.maps.places.Autocomplete($("#address_2")[0], options_2);
google.maps.event.addListener(search2, 'place_changed', function() {
var place = search2.getPlace();
var marker = new google.maps.Marker({
position: place.geometry.location,
map: map,
icon: {
url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
size: new google.maps.Size(7, 7),
anchor: new google.maps.Point(3.5, 3.5)
}
});
});
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 80%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Place Autocomplete</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initAutocomplete&libraries=places&v=weekly" defer></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div class="pac-card" id="pac-card">
<div id="pac-container">
<input id="address_1" type="text" placeholder="Enter a location" /><br/>
<input id="address_2" type="text" placeholder="Enter a location" />
</div>
</div>
<div id="map"></div>
</body>
</html>
推荐阅读
- ios - 在我的应用程序中更新个人资料照片时,使用 firebase 为更新的个人资料照片生成新行
- json - 如何将日期插入 Web 路径 api
- c# - 将参数传递给 .NET Core API 中的自定义类
- scala - 如何定义一个 Scala 函数以在没有括号和点的情况下调用它
- python - Python散点图:无法连接点
- php - 如何在 SQL 上使用准备好的语句
- uisegmentedcontrol - 为什么旋转屏幕(Xcode 11 beta5)时 UISegmentedControl 中选定的段颜色变得清晰?
- azure-sql-database - ADFV2、VNET 和链接服务
- python - 在 Keras 中对 4D 数据执行 Conv2D
- android - 搜索过滤器不在列表视图中显示搜索到的项目