summaryrefslogtreecommitdiff
path: root/src/lib/maps
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/maps')
-rw-r--r--src/lib/maps/components/PinPointMap.jsx43
1 files changed, 36 insertions, 7 deletions
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 = () => {
</div>
<div style={{ marginTop: '20px' }}>
- <Button
- variant='solid'
- onClick={handleUseCurrentLocation}
- >
+ <Button variant='solid' onClick={handleUseCurrentLocation}>
<LocateFixed class='h-6 w-6 text-gray-500 mr-2' /> Gunakan Lokasi Saat
ini
</Button>
@@ -152,8 +178,11 @@ const PinpointLocation = () => {
</div>
<div className='mt-6 flex justify-end'>
- <button className='p-3 border border-red-500 bg-red-600 text-white font-semibold rounded-lg' onClick={handleSavePinpoint}>
- Simpan Lokasi Ini
+ <button
+ className='p-3 border border-red-500 bg-red-600 text-white font-semibold rounded-lg'
+ onClick={handleSavePinpoint}
+ >
+ Simpan Lokasi Ini
</button>
</div>
</div>