[英]Google maps API JS multiple markers using input fields?
I want to be able to show multiple markers when I insert something in to searchboxes on the website. 当我在网站的搜索框中插入内容时,我希望能够显示多个标记。 Right now I have 3 input fields which I want to increase.
现在我要增加3个输入字段。 This is what I currently have, I have tried storing multiple searchBox values in a
var
like so: var markers = searchBox.getPlaces(), searchBox.getPlaces1(), searchBox.getPlaces2()
这是我目前拥有的,我曾尝试将多个searchBox值存储在这样的
var
: var markers = searchBox.getPlaces(), searchBox.getPlaces1(), searchBox.getPlaces2()
How do I extend this code to additional input fields? 如何将此代码扩展到其他输入字段?
<input id="pac-input" class="controls" type="text" placeholder="Search Box" /><br />
<input id="pac-input1" class="controls" type="text" placeholder="Search Box" />
<input id="pac-input2" class="controls" type="text" placeholder="Search box" />
<div id="map"></div>
<script>
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 52.728616, lng: 6.4901 },
zoom: 13,
mapTypeId: 'roadmap'
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var input1 = document.getElementById('pac-input1');
var input2 = document.getElementById('pac-input2');
var searchBox = new google.maps.places.SearchBox(input);
var searchBox1 = new google.maps.places.SearchBox(input1);
var searchBox2 = new google.maps.places.SearchBox(input2);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input1);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
searchBox1.setBounds(map.getBounds());
searchBox2.setBounds(map.getBounds());
});
var markers = [];
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
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.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=[APIKEY]&libraries=places&callback=initAutocomplete"
async defer></script>
related question (for autocomplete): Google Maps API autocomplete 2nd address fields on same page 相关问题(针对自动填充): 同一页面上的Google Maps API自动填充第二个地址字段
Get the array of <input>
elements you want to use for the SearchBox
, use those to create the SearchBox
objects, create a function that takes the unique identifier for the SearchBox
and a reference to the SearchBox
object. 获取要用于
SearchBox
的<input>
元素数组,使用这些元素创建SearchBox
对象,创建一个函数,该函数采用SearchBox
的唯一标识符和对SearchBox
对象的引用。 Use that function to process the events from each of the SearchBox
objects. 使用该函数处理来自每个
SearchBox
对象的事件。
var searchBoxes = document.getElementsByClassName('controls');
for (var i=0; i<searchBoxes.length;i++) {
var searchBox = new google.maps.places.SearchBox(searchBoxes[i]);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchBoxes[i]);
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
markers.push([]);
searchBox.addListener('places_changed', (function(i) {
return function() {
processSearch(i, this)
}
}(i)));
}
processSearch function: processSearch函数:
function processSearch(uniqueId, searchBox) {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers[uniqueId].forEach(function(marker) {
marker.setMap(null);
});
markers[uniqueId] = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
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.
if (!markers[uniqueId]) markers.push([]);
markers[uniqueId].push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
}
code snippet: 代码段:
html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } .controls { width: 100px; }
<input id="pac-input" class="controls" type="text" placeholder="Search Box" /> <input id="pac-input1" class="controls" type="text" placeholder="Search Box" /> <input id="pac-input2" class="controls" type="text" placeholder="Search box" /> <input id="pac-input3" class="controls" type="text" placeholder="Search box" /> <input id="pac-input4" class="controls" type="text" placeholder="Search box" /> <input id="pac-input5" class="controls" type="text" placeholder="Search box" /> <div id="map"></div> <script> function initAutocomplete() { var map = new google.maps.Map(document.getElementById('map'), { center: { lat: 52.728616, lng: 6.4901 }, zoom: 13, mapTypeId: 'roadmap' }); var markers = []; // Create the search boxs and link them to the UI elements. var searchBoxes = document.getElementsByClassName('controls'); for (var i = 0; i < searchBoxes.length; i++) { var searchBox = new google.maps.places.SearchBox(searchBoxes[i]); map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchBoxes[i]); map.addListener('bounds_changed', function() { searchBox.setBounds(map.getBounds()); }); markers.push([]); searchBox.addListener('places_changed', (function(i) { return function() { processSearch(i, this) } }(i))); } function processSearch(uniqueId, searchBox) { var places = searchBox.getPlaces(); if (places.length == 0) { return; } // Clear out the old markers. markers[uniqueId].forEach(function(marker) { marker.setMap(null); }); markers[uniqueId] = []; // For each place, get the icon, name and location. var bounds = new google.maps.LatLngBounds(); places.forEach(function(place) { if (!place.geometry) { console.log("Returned place contains no geometry"); return; } var icon = { 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. if (!markers[uniqueId]) markers.push([]); markers[uniqueId].push(new google.maps.Marker({ map: map, icon: icon, title: place.name, position: place.geometry.location })); if (place.geometry.viewport) { // Only geocodes have viewport. bounds.union(place.geometry.viewport); } else { bounds.extend(place.geometry.location); } }); map.fitBounds(bounds); } } </script> <script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initAutocomplete" async defer></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.