TobiasRothlin / GeoAdminJsApi

A small collection of functions built around the api3 from Geo.Admin.ch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GeoAdminApi3.js

This collection currently contains four functions. These functions are designed to interact with the geo.admin Api3.

To test the functions implemented in this js file. I exported the postman json collection I used and added to this repo (GeoAdminApi.postman_collection.json).

Overview

The four functions are:

async function isValidAddress(searchString);

async function getNumberOfResults(searchString);

async function getListCoordinates(searchString);

async function getRequest(url)

Functions

getRequest(url)

This function is the basic GET request function. The get request will be sent to the url and the response will be server response will be parsed and returns as an json object.

Example:

Response: json

{
    "results": [
        {
            "id": 457481311,
            "weight": 14,
            "attrs": {
                "origin": "address",
                "geom_quadindex": "030031230120230333113",
                "zoomlevel": 10,
                "featureId": "3087703_0",
                "lon": 8.88730239868164,
                "detail": "peterliwiese 33 8855 wangen sz 1349 wangen _sz_ ch sz",
                "rank": 7,
                "geom_st_box2d": "BOX(2709775.421 1228079.091,2709775.421 1228079.091)",
                "lat": 47.194541931152344,
                "num": 33,
                "y": 2709775.5,
                "x": 1228079.125,
                "label": "Peterliwiese 33 <b>8855 Wangen SZ</b>"
            }
        }
    ]
}

getListCoordinates(searchString)

This function will return all the results found with the provided searchString. Internally this function will build the url and call the getRequestfunction.

Example:

searchString: "Peterliwiese 33"

spaces will be encoded!

Response: List

[
    {
        "id": 457481311,
        "weight": 14,
        "attrs": {
            "origin": "address",
            "geom_quadindex": "030031230120230333113",
            "zoomlevel": 10,
            "featureId": "3087703_0",
            "lon": 8.88730239868164,
            "detail": "peterliwiese 33 8855 wangen sz 1349 wangen _sz_ ch sz",
            "rank": 7,
            "geom_st_box2d": "BOX(2709775.421 1228079.091,2709775.421 1228079.091)",
            "lat": 47.194541931152344,
            "num": 33,
            "y": 2709775.5,
            "x": 1228079.125,
            "label": "Peterliwiese 33 <b>8855 Wangen SZ</b>"
        }
    }
]

getNumberOfResults(searchString)

This function will return the number of results found with the provided searchString. Internally call the getListCoordinatesfunction and returns the length of the result.

Example:

searchString: "Peterliwiese 33"

spaces will be encoded!

Response: number

1

isValidAddress(searchString)

This function will return the number of results found with the provided searchString. Internally call the getNumberOfResultsfunction and if the result has only one element it will return true.

Example:

searchString: "Peterliwiese 33"

spaces will be encoded!

Response: boolean

true

Usage

I wrote a small HTML page (example.html) to show how to call the functions. The results of the calls wil be outputted to the console.

import GeoAdminApi3.js

<script src="GeoAdminApi3.js"></script>

call a function

let searchString = document.getElementById("getListCoordinatesTextField").value;
let result = await getListCoordinates(searchString);
console.log(result);
//Do Stuff with the result!

About

A small collection of functions built around the api3 from Geo.Admin.ch


Languages

Language:HTML 79.2%Language:JavaScript 20.8%