-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from r00tat/feature/place-search
Geocoding von Adressen und Orten
- Loading branch information
Showing
7 changed files
with
105 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export interface OSMPlace { | ||
place_id: number; | ||
licence: string; | ||
osm_type: string; | ||
osm_id: number; | ||
lat: string; | ||
lon: string; | ||
category?: string; | ||
type?: string; | ||
place_rank: number; | ||
importance: number; | ||
addresstype?: string; | ||
name: string; | ||
display_name: string; | ||
boundingbox: [number, number, number, number]; | ||
distance?: number; | ||
} | ||
|
||
export interface PlacesResponse { | ||
places?: OSMPlace[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,48 @@ | ||
import { google } from "googleapis"; | ||
import haversine from "haversine-distance"; | ||
import { defaultGeoPosition, GeoPosition } from "../../../common/geo"; | ||
import { OSMPlace } from "../../../common/osm"; | ||
|
||
export async function searchPlace( | ||
query: string, | ||
{ | ||
position, | ||
maxResults = 3, | ||
}: { | ||
position?: GeoPosition; | ||
maxResults?: number; | ||
} = {} | ||
) { | ||
const auth = new google.auth.GoogleAuth({ | ||
scopes: ["https://www.googleapis.com/auth/cloud-platform"], | ||
const uri = `https://nominatim.openstreetmap.org/search?${new URLSearchParams( | ||
{ | ||
q: `${query}, Österreich`, | ||
format: "jsonv2", | ||
limit: "10", | ||
} | ||
)}`; | ||
// console.info(`uri: ${uri}`); | ||
const result = await fetch(uri, { | ||
headers: { | ||
"User-Agent": "Hydrantenkarte https://hydrant.ffnd.at", | ||
Accept: "application/json", | ||
}, | ||
}); | ||
|
||
const places = google.places({ version: "v1", auth }); | ||
const bodyText = await result.text(); | ||
if (result.status !== 200) { | ||
throw new Error(`Geocoding failed ${result.status} ${bodyText}`); | ||
} | ||
|
||
const result = ( | ||
await places.places.searchText({ | ||
fields: | ||
// "places(id,formattedAddress,internationalPhoneNumber,location,googleMapsUri,iconBackgroundColor,displayName(text))", | ||
"*", | ||
console.info(`geocoding result: ${result.status} ${bodyText}`); | ||
const results: OSMPlace[] = JSON.parse(bodyText); | ||
|
||
requestBody: { | ||
maxResultCount: 3, | ||
languageCode: "de", | ||
textQuery: query, | ||
locationBias: { | ||
circle: { | ||
center: { | ||
latitude: (position || defaultGeoPosition).lat, | ||
longitude: (position || defaultGeoPosition).lng, | ||
}, | ||
radius: 30000, | ||
}, | ||
}, | ||
}, | ||
}) | ||
).data.places; | ||
results.forEach( | ||
(p) => | ||
(p.distance = haversine( | ||
{ lat: Number.parseFloat(p.lat), lon: Number.parseFloat(p.lon) }, | ||
(position || defaultGeoPosition)?.toGeoObject() | ||
)) | ||
); | ||
results.sort((a, b) => (a.distance || 0) - (b.distance || 0)); | ||
|
||
return result; | ||
return results.slice(0, maxResults); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters