Created
October 24, 2024 14:20
-
-
Save acoyfellow/1b12a0ca1d061527fc401df674dc01b8 to your computer and use it in GitHub Desktop.
Phonesites Google Nearby Map Example
This file contains hidden or 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
<!-- | |
place this in a Code block | |
--> | |
<script async | |
src="https://maps.googleapis.com/maps/api/js?key=*******&libraries=places&callback=initMap"> | |
</script> | |
<div style="height: 500px;" id="map"></div> | |
<script> | |
let map; | |
async function initMap() { | |
const { Map, InfoWindow } = await google.maps.importLibrary("maps"); | |
let center = new google.maps.LatLng(52.369358, 4.889258); | |
map = new Map(document.getElementById("map"), { | |
center: center, | |
zoom: 11, | |
mapId: "DEMO_MAP_ID", | |
}); | |
nearbySearch(); | |
} | |
async function nearbySearch() { | |
//@ts-ignore | |
const { Place, SearchNearbyRankPreference } = await google.maps.importLibrary( | |
"places", | |
); | |
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); | |
// Restrict within the map viewport. | |
let center = new google.maps.LatLng(52.369358, 4.889258); | |
const request = { | |
// required parameters | |
fields: ["displayName", "location", "businessStatus"], | |
locationRestriction: { | |
center: center, | |
radius: 500, | |
}, | |
// optional parameters | |
includedPrimaryTypes: ["restaurant"], | |
maxResultCount: 5, | |
rankPreference: SearchNearbyRankPreference.POPULARITY, | |
language: "en-US", | |
region: "us", | |
}; | |
//@ts-ignore | |
const { places } = await Place.searchNearby(request); | |
if (places.length) { | |
console.log(places); | |
const { LatLngBounds } = await google.maps.importLibrary("core"); | |
const bounds = new LatLngBounds(); | |
// Loop through and get all the results. | |
places.forEach((place) => { | |
const markerView = new AdvancedMarkerElement({ | |
map, | |
position: place.location, | |
title: place.displayName, | |
}); | |
bounds.extend(place.location); | |
console.log(place); | |
}); | |
map.fitBounds(bounds); | |
} else { | |
console.log("No results"); | |
} | |
} | |
initMap(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment