<div class="form-group">
<label for="pacinput" class="control-label col-md-4">Location Search</label>
<div class="col-md-8">
<input id="pacinput" class="form-control" type="text" placeholder="Search Location">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-8">
<asp:HiddenField runat="server" ID="address" />
<asp:HiddenField ID="txtLat" runat="server" />
<asp:HiddenField ID="txtLng" runat="server" />
<div id="map" style="width: 100%; height: 380px;">
</div>
</div>
</div>
// This instance introduces a search input on a map, utilizing Google Place Autocomplete
// feature for geographical searches. Users can enter locations in the search box and it will return
// suggestions containing places or predicted terms.
// To use this functionality, ensure to include the Places library. Add libraries=places
// parameter when initially loading the API like:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 15.362813, lng: 75.126129 },
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pacinput');
var searchBox = new google.maps.places.SearchBox(input);
// Adjust the SearchBox results based on the current viewport of the map.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for user selection event and fetch more details about the place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear previous markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// Get icon, name, and location for each place.
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
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)
};
// Generate 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) {
// Check if there's a viewport available.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>