Hi i'm using Google Places Autocomplete with 4 inputs. The problem is that when a single input field is changed the previous marker(and its infowindow) of this input field doesn't disappear from the map. How can I make the markers to be overwritten? Any answer is appreciated.
Here is my code:
<script>
var map = null;
var autocompleteOptions = {
componentRestrictions: {country: "az"}
};
Here are the arrays for markers and infowindows:
var infowindow = [];
var marker = [];
setupAutocomplete function is called for every input field:
function setupAutocomplete(autocomplete,inputs,i) {
autocomplete[i] = new google.maps.places.Autocomplete(inputs[i], autocompleteOptions);
autocomplete[i].bindTo('bounds', map);
google.maps.event.addListener(autocomplete[i], 'place_changed', function() {
infowindow[i] = new google.maps.InfoWindow();
marker[i] = new google.maps.Marker({
map: map
});
infowindow[i].close();
marker[i].setVisible(false);
var place = autocomplete[i].getPlace();
if (!place.geometry) {
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker[i].setIcon(/** @type {google.maps.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(35, 35)
}));
marker[i].setPosition(place.geometry.location);
marker[i].setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindow[i].setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow[i].open(map, marker[i]);
});
}
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.4700, 49.9600),
zoom: 10,
zoomControl:true,
zoomControlOptions: {
style:google.maps.ZoomControlStyle.SMALL
},
mapTypeControl:true,
mapTypeControlOptions: {
style:google.maps.MapTypeControlStyle.DROPDOWN_MENU
}
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var types = document.getElementById('type-selector');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(types);
var inputs = document.getElementsByClassName("controlsInput");
var autocomplete = [];
for(var i = 0; i < inputs.length; i++) {
setupAutocomplete(autocomplete,inputs,i);
}
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener(id, types) {
var radioButton = document.getElementById(id);
google.maps.event.addDomListener(radioButton, 'click', function() {
for (var i=0 ; i<autocomplete.length; i++) {
autocomplete[i].setTypes(types);
}
});
}
setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<input id="pac-input" class="controlsInput" type="text" placeholder="Enter the pickup point"><br>
<input id="pac-input2" class="controlsInput" type="text" placeholder="Enter your destination"><br>
<input id="pac-input3" class="controlsInput" type="text" placeholder="Enter your destination"><br>
<input id="pac-input4" class="controlsInput" type="text" placeholder="Enter your destination"><br><br>
<div hidden="hidden" id="type-selector" class="controls">
<input type="radio" name="type" id="changetype-all" checked="checked">
<label for="changetype-all">All</label>
<input type="radio" name="type" id="changetype-establishment">
<label for="changetype-establishment">Establishments</label>
<input type="radio" name="type" id="changetype-geocode">
<label for="changetype-geocode">Geocodes</label>
</div>
<div id="map-canvas" style="width:600px;height:380px;"></div>
</body>
</html>