From bc4fe87e012cc1b06572ca12f1a3b92f6d1757e0 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Thu, 31 Oct 2024 09:09:05 +0700 Subject: detail adress --- src/lib/maps/components/PinPointMap.jsx | 43 +++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'src/lib/maps/components/PinPointMap.jsx') diff --git a/src/lib/maps/components/PinPointMap.jsx b/src/lib/maps/components/PinPointMap.jsx index 0781d8a8..1fa40036 100644 --- a/src/lib/maps/components/PinPointMap.jsx +++ b/src/lib/maps/components/PinPointMap.jsx @@ -8,6 +8,7 @@ import { import { useMaps } from '../stores/useMaps'; import { LocateFixed, MapPinIcon } from 'lucide-react'; import { Button } from '@chakra-ui/react'; +import { useForm } from 'react-hook-form'; const containerStyle = { width: '100%', @@ -30,6 +31,7 @@ const PinpointLocation = () => { const [tempAddress, setTempAddress] = useState(''); const [tempPosition, setTempPosition] = useState(center); + const { setValue } = useForm(); const autocompleteRef = useRef(null); @@ -50,6 +52,11 @@ const PinpointLocation = () => { } }; + const getAddressComponent = (components, type) => { + const component = components.find((comp) => comp.types.includes(type)); + return component ? component.long_name : ''; + }; + const getAddress = async (lat, lng) => { try { const response = await fetch( @@ -57,6 +64,28 @@ const PinpointLocation = () => { ); const data = await response.json(); if (data.results[0]) { + const addressComponents = data.results[0].address_components; + const details = { + province: getAddressComponent( + addressComponents, + 'administrative_area_level_1' + ), + district: getAddressComponent( + addressComponents, + 'administrative_area_level_2' + ), + subDistrict: getAddressComponent( + addressComponents, + 'administrative_area_level_3' + ), + village: getAddressComponent( + addressComponents, + 'administrative_area_level_4' + ), + postalCode: getAddressComponent(addressComponents, 'postal_code'), + }; + setValue('state', details?.province); + console.log(details); setTempAddress(data.results[0].formatted_address); } } catch (error) { @@ -82,7 +111,7 @@ const PinpointLocation = () => { const handleSavePinpoint = (event) => { event.preventDefault(); - if(tempAddress === '') { + if (tempAddress === '') { alert('Silahkan pilih lokasi terlebih dahulu'); return; } @@ -134,10 +163,7 @@ const PinpointLocation = () => {
- @@ -152,8 +178,11 @@ const PinpointLocation = () => {
-
-- cgit v1.2.3