Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use advanced places api #7

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/routes/Geolocator.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
let geocoder: google.maps.Geocoder;
let map: google.maps.Map;
let mapElement: HTMLElement;
let locationSearch: string = '';
const marker = writable<google.maps.marker.AdvancedMarkerElement | null>(null);
const showDebug = writable(false);
const showMap = writable(false);
Expand All @@ -25,6 +24,17 @@
let zip = '';
let nation = '';

interface PlaceSelectEvent extends Event {
place: {
fetchFields: (options: { fields: string[] }) => Promise<void>;
location: {
lng: () => number;
lat: () => number;
};
formattedAddress: string;
};
}

const createMarker = (mapInstance: google.maps.Map, position: google.maps.LatLngLiteral): void => {
const markerInstance = new google.maps.marker.AdvancedMarkerElement({
map: mapInstance,
Expand Down Expand Up @@ -164,6 +174,7 @@

mapElement = document.getElementById('map') as HTMLElement;
geocoder = new google.maps.Geocoder();

if (mapElement) {
map = new Map(mapElement, {
center: { lat: 37, lng: -96 },
Expand All @@ -179,22 +190,27 @@
}
});

if (typeof window !== 'undefined') {
const autocomplete = new google.maps.places.Autocomplete(document.getElementById('locationSearch') as HTMLInputElement, { types: ['geocode'] });
autocomplete.bindTo('bounds', map);
autocomplete.addListener('place_changed', () => {
const place = autocomplete.getPlace();
if (!place.geometry) return;
const location = place.geometry.location;
if (location) {
longitude.set(location.lng().toString());
latitude.set(location.lat().toString());
const latLngLiteral = { lat: location.lat(), lng: location.lng() };
createMarker(map, latLngLiteral);
map.setCenter(latLngLiteral);
}
const autocomplete = new google.maps.places.PlaceAutocompleteElement({});
autocomplete.id = 'locationSearch';
const locationSearchDiv = document.getElementById('locationSearch') as HTMLElement;
locationSearchDiv.appendChild(autocomplete);
autocomplete.addEventListener('gmp-placeselect', async (event) => {
const placeEvent = event as PlaceSelectEvent;
const place = placeEvent.place;
await place.fetchFields({
fields: ['displayName', 'formattedAddress', 'location']
});
}
addressString.set(place.formattedAddress);
const location = place.location;
if (location) {
longitude.set(location.lng().toString());
latitude.set(location.lat().toString());
const latLngLiteral = { lat: location.lat(), lng: location.lng() };
createMarker(map, latLngLiteral);
map.setCenter(latLngLiteral);
reverseLookup();
}
});
}
});
</script>
Expand All @@ -205,7 +221,7 @@
<form on:submit|preventDefault={() => geocodeAddress()}>
<div class="one_line_form">
<label for="locationSearch">Location Search:</label>
<input id="locationSearch" bind:value={locationSearch} />
<div id="locationSearch"></div>
</div>
<div class="one_line_form">
<label for="address">Address:</label>
Expand Down
8 changes: 8 additions & 0 deletions src/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ a:active {
flex: 1 1 50%;
}

div.widget-container div.input-container input {
flex: 1 1 50%;
}

@media (max-width: 768px) {
#bmlt_tools_main_container {
width: 95%;
Expand Down Expand Up @@ -295,6 +299,10 @@ a:active {
#bmlt_tools_main_container .one_line_form input#locationSearch {
width: 50%;
}

div.widget-container div.input-container input {
width: 50%;
}
}

/* Dark mode styles */
Expand Down