gpt4 book ai didi

javascript - 将 google maps api 搜索的范围限制在一个区域

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:12 31 4
gpt4 key购买 nike

我想将我的谷歌地图的搜索结果限制在特定的 latLngBounds 范围内。

places search api - 列出此 addListener 函数以更改边界:

google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});

我将其替换为:

var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));

searchBox.setBounds(defaultBounds);

不过好像不行。无论当前 map 视点如何,任何人都可以指出正确的方向将搜索结果限制在默认范围内吗?

这是我基于 API 的当前代码:

function initialize() {

var markers = [];
var map = new google.maps.Map(document.getElementById('map-canvas'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(30.9128292, -98.8563538),
new google.maps.LatLng(30.0964251, -97.9431152));
map.fitBounds(defaultBounds);

// Create the search box and link it to the UI element.
var input = /** @type {HTMLInputElement} */(
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));

// [START region_getplaces]
// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();

if (places.length == 0) {
return;
}
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}

// For each place, get the icon, place name, and location.
markers = [];
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};

// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});

markers.push(marker);

bounds.extend(place.geometry.location);
}

map.fitBounds(bounds);
});
// [END region_getplaces]

searchBox.setBounds(defaultBounds);
}

google.maps.event.addDomListener(window, 'load', initialize);

最佳答案

这不是“改变”边界,而是在边界改变时监听:

google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});

{bound : new LatLngBounds.... } 传递给:

new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));

像这样

拥有你的边界对象:

 var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));

传递给构造函数:

new google.maps.places.SearchBox(input, {bounds: defaultBounds});

查看 google.maps.places.SearchBox 的引用资料

关于javascript - 将 google maps api 搜索的范围限制在一个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304014/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com