ask for location permission

This commit is contained in:
2025-12-02 01:16:29 +03:00
parent b476bc5ebb
commit cf5babeaa5
3 changed files with 137 additions and 13 deletions

View File

@@ -85,25 +85,85 @@ function MapComponent({
});
markerRef.current = initialMarker;
// Geocode the initial location if address is not provided
if (!initialLocation.address && onLocationSelect) {
const geocoder = new google.maps.Geocoder();
geocoder.geocode(
{
location: {
lat: initialLocation.lat,
lng: initialLocation.lng,
},
},
(results, status) => {
if (status === "OK" && results && results[0]) {
const address = results[0].formatted_address;
console.log(
"Initial location geocoded - calling onLocationSelect:",
{
lat: initialLocation.lat,
lng: initialLocation.lng,
address,
},
);
onLocationSelect(
initialLocation.lat,
initialLocation.lng,
address,
);
} else {
console.log("Geocoding failed on initial location:", status);
// Fallback: use coordinates as address if geocoding fails
const fallbackAddress = `Location: ${initialLocation.lat.toFixed(6)}, ${initialLocation.lng.toFixed(6)}`;
console.log(
"Using fallback address for initial location:",
fallbackAddress,
);
onLocationSelect(
initialLocation.lat,
initialLocation.lng,
fallbackAddress,
);
}
},
);
} else if (initialLocation.address && onLocationSelect) {
// If address is already provided, just call onLocationSelect
onLocationSelect(
initialLocation.lat,
initialLocation.lng,
initialLocation.address,
);
}
// Add drag end listener to the initial marker
initialMarker.addListener("dragend", () => {
const position = initialMarker.getPosition();
if (position) {
const lat = position.lat();
const lng = position.lng();
// Get address from coordinates
const geocoder = new google.maps.Geocoder();
geocoder.geocode({ location: { lat, lng } }, (results, status) => {
if (status === "OK" && results && results[0]) {
const address = results[0].formatted_address;
console.log('Initial marker drag - calling onLocationSelect:', { lat, lng, address });
console.log(
"Initial marker drag - calling onLocationSelect:",
{ lat, lng, address },
);
onLocationSelect?.(lat, lng, address);
} else {
console.log('Geocoding failed on initial marker drag:', status);
console.log(
"Geocoding failed on initial marker drag:",
status,
);
// Fallback: use coordinates as address if geocoding fails
const fallbackAddress = `Location: ${lat.toFixed(6)}, ${lng.toFixed(6)}`;
console.log('Using fallback address for initial marker drag:', fallbackAddress);
console.log(
"Using fallback address for initial marker drag:",
fallbackAddress,
);
onLocationSelect?.(lat, lng, fallbackAddress);
}
});

View File

@@ -11,6 +11,7 @@ interface MapBottomSheetProps {
onClose: () => void;
initialValue: string;
onSave: (value: string) => void;
initialLocation?: { lat: number; lng: number; address?: string };
}
interface LocationData {
@@ -24,6 +25,7 @@ export function MapBottomSheet({
onClose,
initialValue,
onSave,
initialLocation,
}: MapBottomSheetProps) {
const { t } = useTranslation();
const [value, setValue] = useState(initialValue);
@@ -35,8 +37,15 @@ export function MapBottomSheet({
useEffect(() => {
setValue(initialValue);
// Try to parse initialValue as JSON to set selectedLocation
if (initialValue) {
// Priority: initialLocation > parsed initialValue
if (initialLocation) {
setSelectedLocation({
lat: initialLocation.lat,
lng: initialLocation.lng,
address: initialLocation.address || "",
});
} else if (initialValue) {
// Try to parse initialValue as JSON to set selectedLocation
try {
const parsed = JSON.parse(initialValue);
if (parsed.lat && parsed.lng && parsed.address) {
@@ -47,7 +56,7 @@ export function MapBottomSheet({
setSelectedLocation(null);
}
}
}, [initialValue]);
}, [initialValue, initialLocation]);
const handleLocationSelect = (lat: number, lng: number, address: string) => {
console.log('Location selected:', { lat, lng, address });
@@ -99,7 +108,11 @@ export function MapBottomSheet({
initialAddress={selectedLocation?.address || ""}
/>
) : (
<GoogleMap onLocationSelect={handleLocationSelect} height="100%" />
<GoogleMap
onLocationSelect={handleLocationSelect}
height="100%"
initialLocation={selectedLocation || initialLocation}
/>
)}
</div>
</div>