JSFiddle - React, Tailwind, and code Playground
by Faisal Khan Janjua
HTML
<!DOCTYPE html>
<html>
<head>
<title>Geolocation and Google Maps (Mock)</title>
<style>
#autocomplete {
width: 300px;
padding: 10px;
}
.address-field {
margin-top: 10px;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
// Mocking the autocomplete and geolocation functionality for testing purposes
const mockPlaces = [
{ formatted_address: '1600 Amphitheatre Parkway, Mountain View, CA', components: { street_number: '1600', route: 'Amphitheatre Parkway', locality: 'Mountain View', administrative_area_level_1: 'CA', postal_code: '94043', country: 'US' } },
{ formatted_address: '1 Infinite Loop, Cupertino, CA', components: { street_number: '1', route: 'Infinite Loop', locality: 'Cupertino', administrative_area_level_1: 'CA', postal_code: '95014', country: 'US' } }
];
let autocompleteInput = document.getElementById('autocomplete');
autocompleteInput.addEventListener('input', (event) => {
let inputVal = event.target.value.toLowerCase();
let matchedPlace = mockPlaces.find(place => place.formatted_address.toLowerCase().includes(inputVal));
if (matchedPlace) {
fillInAddress(matchedPlace);
}
});
// Mock geolocation
function geolocate() {
console.log("Mock geolocation called");
// Normally this would get the user's current location, but we will skip this for the mock
}
// Mock fillInAddress function
function fillInAddress(place) {
console.log('Mock place:', place);
let address = `${place.components.street_number} ${place.components.route}`;
document.getElementById('autocomplete').value = address;
...