GOOGLE MAPS

Google Maps

Google Maps

Blog Article

Google Maps
copyright src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">copyright>// function initMap() { // Map centered on a default location const map = new google.maps.Map(document.getElementById("map"), { center: { lat: 40.7128, lng: -74.0060 }, // Default to New York City zoom: 13, }); // Add search box const input = document.getElementById("search-input"); const searchBox = new google.maps.places.SearchBox(input); // Bias the SearchBox results towards current map's viewport. map.addListener("bounds_changed", () => {
searchBox.setBounds(map.getBounds());
});

// Place markers on the map as per search results
let markers = [];
searchBox.addListener("places_changed", () => {
const places = searchBox.getPlaces();

if (places.length == 0) {
return;
}

// Clear out the old markers.
markers.forEach((marker) => {
marker.setMap(null);
});
markers = [];

// For each place, get the icon, name, and location.
const bounds = new google.maps.LatLngBounds();
places.forEach((place) => {
if (!place.geometry || !place.geometry.location) {
console.log("Returned place contains no geometry");
return;
}

// Create a marker for each place.
markers.push(
new google.maps.Marker({
map,
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);
});
}
// ]]>


Report this page