mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-23 06:49:37 +02:00
Implement reverse geocoding search functionality and update type definitions
This commit is contained in:
parent
ec2b285d50
commit
042d034594
5 changed files with 44 additions and 32 deletions
|
@ -27,6 +27,32 @@ class ReverseGeocodeViewSet(viewsets.ViewSet):
|
||||||
if 'error' in data:
|
if 'error' in data:
|
||||||
return Response({"error": "An internal error occurred while processing the request"}, status=400)
|
return Response({"error": "An internal error occurred while processing the request"}, status=400)
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
|
@action(detail=False, methods=['get'])
|
||||||
|
def search(self, request):
|
||||||
|
query = request.query_params.get('query', '')
|
||||||
|
if not query:
|
||||||
|
return Response({"error": "Query parameter is required"}, status=400)
|
||||||
|
url = f"https://nominatim.openstreetmap.org/search?q={query}&format=jsonv2"
|
||||||
|
headers = {'User-Agent': 'AdventureLog Server'}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
try:
|
||||||
|
data = response.json()
|
||||||
|
parsed_results = []
|
||||||
|
for item in data:
|
||||||
|
parsed_results.append({
|
||||||
|
"lat": item.get("lat"),
|
||||||
|
"lon": item.get("lon"),
|
||||||
|
"category": item.get("category"),
|
||||||
|
"type": item.get("type"),
|
||||||
|
"importance": item.get("importance"),
|
||||||
|
"addresstype": item.get("addresstype"),
|
||||||
|
"name": item.get("name"),
|
||||||
|
"display_name": item.get("display_name"),
|
||||||
|
})
|
||||||
|
except requests.exceptions.JSONDecodeError:
|
||||||
|
return Response({"error": "Invalid response from geocoding service"}, status=400)
|
||||||
|
return Response(parsed_results)
|
||||||
|
|
||||||
@action(detail=False, methods=['post'])
|
@action(detail=False, methods=['post'])
|
||||||
def mark_visited_region(self, request):
|
def mark_visited_region(self, request):
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { appVersion } from '$lib/config';
|
import { appVersion } from '$lib/config';
|
||||||
import { addToast } from '$lib/toasts';
|
import { addToast } from '$lib/toasts';
|
||||||
import type { Adventure, Lodging, OpenStreetMapPlace, Point, ReverseGeocode } from '$lib/types';
|
import type { Adventure, Lodging, GeocodeSearchResult, Point, ReverseGeocode } from '$lib/types';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { t } from 'svelte-i18n';
|
import { t } from 'svelte-i18n';
|
||||||
import { DefaultMarker, MapEvents, MapLibre } from 'svelte-maplibre';
|
import { DefaultMarker, MapEvents, MapLibre } from 'svelte-maplibre';
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
let willBeMarkedVisited: boolean = false;
|
let willBeMarkedVisited: boolean = false;
|
||||||
let previousCoords: { lat: number; lng: number } | null = null;
|
let previousCoords: { lat: number; lng: number } | null = null;
|
||||||
let old_display_name: string = '';
|
let old_display_name: string = '';
|
||||||
let places: OpenStreetMapPlace[] = [];
|
let places: GeocodeSearchResult[] = [];
|
||||||
let noPlaces: boolean = false;
|
let noPlaces: boolean = false;
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
@ -167,13 +167,9 @@
|
||||||
alert($t('adventures.no_location'));
|
alert($t('adventures.no_location'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`, {
|
let res = await fetch(`/api/reverse-geocode/search/?query=${query}`);
|
||||||
headers: {
|
|
||||||
'User-Agent': `AdventureLog / ${appVersion} `
|
|
||||||
}
|
|
||||||
});
|
|
||||||
console.log(res);
|
console.log(res);
|
||||||
let data = (await res.json()) as OpenStreetMapPlace[];
|
let data = (await res.json()) as GeocodeSearchResult[];
|
||||||
places = data;
|
places = data;
|
||||||
if (data.length === 0) {
|
if (data.length === 0) {
|
||||||
noPlaces = true;
|
noPlaces = true;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
import type { Adventure, OpenStreetMapPlace, Point } from '$lib/types';
|
import type { Adventure, GeocodeSearchResult, Point } from '$lib/types';
|
||||||
import { createEventDispatcher } from 'svelte';
|
import { createEventDispatcher } from 'svelte';
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
@ -50,7 +50,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let places: OpenStreetMapPlace[] = [];
|
let places: GeocodeSearchResult[] = [];
|
||||||
|
|
||||||
async function geocode(e: Event | null) {
|
async function geocode(e: Event | null) {
|
||||||
if (e) {
|
if (e) {
|
||||||
|
@ -60,13 +60,9 @@
|
||||||
alert('Please enter a location');
|
alert('Please enter a location');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`, {
|
let res = await fetch(`/api/reverse-geocode/search/?query=${query}`);
|
||||||
headers: {
|
|
||||||
'User-Agent': `AdventureLog / ${appVersion} `
|
|
||||||
}
|
|
||||||
});
|
|
||||||
console.log(res);
|
console.log(res);
|
||||||
let data = (await res.json()) as OpenStreetMapPlace[];
|
let data = (await res.json()) as GeocodeSearchResult[];
|
||||||
places = data;
|
places = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchLocation = async (query: string) => {
|
const fetchLocation = async (query: string) => {
|
||||||
let res = await fetch(`https://nominatim.openstreetmap.org/search?q=${query}&format=jsonv2`, {
|
let res = await fetch(`/api/reverse-geocode/search/?query=${query}`, {
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': `AdventureLog / ${appVersion} `
|
'User-Agent': `AdventureLog / ${appVersion} `
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,21 +139,15 @@ export type Collection = {
|
||||||
link?: string | null;
|
link?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OpenStreetMapPlace = {
|
export type GeocodeSearchResult = {
|
||||||
place_id: number;
|
lat?: string;
|
||||||
licence: string;
|
lon?: string;
|
||||||
osm_type: string;
|
category?: string;
|
||||||
osm_id: number;
|
type?: string;
|
||||||
lat: string;
|
importance?: number;
|
||||||
lon: string;
|
addresstype?: string;
|
||||||
category: string;
|
name?: string;
|
||||||
type: string;
|
display_name?: string;
|
||||||
place_rank: number;
|
|
||||||
importance: number;
|
|
||||||
addresstype: string;
|
|
||||||
name: string;
|
|
||||||
display_name: string;
|
|
||||||
boundingbox: string[];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Transportation = {
|
export type Transportation = {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue