-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
524 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useEffect } from "react"; | ||
|
||
const GoogleMap = () => { | ||
useEffect(() => { | ||
const initAutocomplete = () => { | ||
const map = new google.maps.Map(document.getElementById("map"), { | ||
center: { lat: -33.8688, lng: 151.2195 }, | ||
zoom: 13, | ||
mapTypeId: "roadmap", | ||
}); | ||
|
||
const input = document.getElementById("pac-input"); | ||
const searchBox = new google.maps.places.SearchBox(input); | ||
|
||
// map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); | ||
|
||
map.addListener("bounds_changed", () => { | ||
searchBox.setBounds(map.getBounds()); | ||
}); | ||
|
||
let markers = []; | ||
|
||
searchBox.addListener("places_changed", () => { | ||
const places = searchBox.getPlaces(); | ||
|
||
if (places.length === 0) { | ||
return; | ||
} | ||
|
||
markers.forEach((marker) => { | ||
marker.setMap(null); | ||
}); | ||
markers = []; | ||
|
||
const bounds = new google.maps.LatLngBounds(); | ||
|
||
places.forEach((place) => { | ||
if (!place.geometry || !place.geometry.location) { | ||
console.log("Returned place contains no geometry"); | ||
return; | ||
} | ||
|
||
const 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), | ||
}; | ||
|
||
markers.push( | ||
new google.maps.Marker({ | ||
map, | ||
icon, | ||
title: place.name, | ||
position: place.geometry.location, | ||
}) | ||
); | ||
|
||
if (place.geometry.viewport) { | ||
bounds.union(place.geometry.viewport); | ||
} else { | ||
bounds.extend(place.geometry.location); | ||
} | ||
}); | ||
|
||
map.fitBounds(bounds); | ||
}); | ||
}; | ||
|
||
const googleMapScript = document.createElement("script"); | ||
googleMapScript.src = `https://maps.googleapis.com/maps/api/js?key=${process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY}&libraries=places`; | ||
googleMapScript.async = true; | ||
googleMapScript.defer = true; | ||
googleMapScript.addEventListener("load", initAutocomplete); | ||
|
||
document.head.appendChild(googleMapScript); | ||
}, []); | ||
|
||
return <div id="map" className="h-96 mt-4" />; | ||
}; | ||
|
||
export default GoogleMap; |
Oops, something went wrong.