UNPKG

expo-osm-sdk

Version:

OpenStreetMap component for React Native with Expo

112 lines 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useNominatimSearch = void 0; const react_1 = require("react"); const nominatim_1 = require("../utils/nominatim"); /** * React Hook for Nominatim Search * * Provides search and reverse geocoding functionality with state management * * @returns UseNominatimSearchReturn * * @example * ```tsx * function MyComponent() { * const { search, reverseGeocode, isLoading, error, lastResults } = useNominatimSearch(); * * const handleSearch = async () => { * try { * const results = await search('New York'); * console.log('Found locations:', results); * } catch (err) { * console.error('Search failed:', err); * } * }; * * return ( * <View> * <Button title="Search" onPress={handleSearch} /> * {isLoading && <Text>Searching...</Text>} * {error && <Text>Error: {error}</Text>} * {lastResults.map(result => ( * <Text key={result.placeId}>{result.displayName}</Text> * ))} * </View> * ); * } * ``` */ const useNominatimSearch = () => { const [isLoading, setIsLoading] = (0, react_1.useState)(false); const [error, setError] = (0, react_1.useState)(null); const [lastResults, setLastResults] = (0, react_1.useState)([]); /** * Search for locations by text query */ const search = (0, react_1.useCallback)(async (query, options) => { if (!query.trim()) { setLastResults([]); return []; } setIsLoading(true); setError(null); try { const results = await (0, nominatim_1.searchLocations)(query, options); setLastResults(results); return results; } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Search failed'; setError(errorMessage); setLastResults([]); throw err; } finally { setIsLoading(false); } }, []); /** * Reverse geocode coordinates to get location details */ const reverseGeocodeLocation = (0, react_1.useCallback)(async (coordinate, options) => { setIsLoading(true); setError(null); try { const result = await (0, nominatim_1.reverseGeocode)(coordinate, options); if (result) { setLastResults([result]); } else { setLastResults([]); } return result; } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Reverse geocoding failed'; setError(errorMessage); setLastResults([]); throw err; } finally { setIsLoading(false); } }, []); /** * Clear current results and error state */ const clearResults = (0, react_1.useCallback)(() => { setLastResults([]); setError(null); }, []); return { search, reverseGeocode: reverseGeocodeLocation, isLoading, error, lastResults, clearResults, }; }; exports.useNominatimSearch = useNominatimSearch; //# sourceMappingURL=useNominatimSearch.js.map