Distance Svizzera
BetaReturn driving distance and time between two Swiss postcodes.
https://api.pro6pp.nl/v2/distance/chPanoramica
Returns the distance between a start and a destination postcode. With the road algorithm the distance is over the road in meters and includes the car drive time in minutes.
Both postcodes must be 4 digits long.
ParametriQuery string
A sixpp or a fourpp to calculate to.
- Esempio
4145- Pattern
^([0-9]{4})$|^([0-9]{4} ?[a-zA-Z]{2})$
A sixpp or a fourpp to start calculating from.
- Esempio
4146- Pattern
^([0-9]{4})$|^([0-9]{4} ?[a-zA-Z]{2})$
Specifies the algorithm for calculating distance and time.
Available Algorithms:
straight: Calculates the distance in a straight line.road: Calculates the actual traveling distance and time using the drive time matrix (shortest route) over the actual road network.- Availability of
roaddepends on your subscription level. - For trial purposes, this algorithm can be used with any subscription level for the following postcodes:
4146(Hochwald),4147(Aesch) and4148(Pfeffingen).
- Availability of
- Esempio
road- Predefinito
"straight"- Uno tra
straightroad
Risposte
| Campo | Tipo | Valore di esempio | Descrizione |
|---|---|---|---|
| distance | number | 3346 | Distance in metres between the two postal codes: over the road network for the road algorithm, in a straight line otherwise; 0 for the same postal code. |
| duration | numberNullable | 4 | Driving time in whole minutes, rounded down, for the road algorithm; absent for the straight algorithm. |
{
"distance": 3346,
"duration": 4
}Errori
Campi di una risposta di errore
| Campo | Tipo | Descrizione |
|---|---|---|
| error_id | string | The reason the request failed, such as |
| errors | array of object | One entry per problem found in the request. |
| errors[].message | string | A readable explanation of the problem, in English. |
| errors[].help | string | A link to the documentation section that covers the problem. |
| errors[].parameter | any | The parameter the problem is about. Usually its name; a list when several parameters are involved, or a name with the values to choose from when the request must select one. |
| errors[].value | any | The value the request sent for |
INVALID_VALUEA postal code is missing, malformed or cannot be found, or algorithm is not straight or road.
A postal code is missing, malformed or cannot be found, or algorithm is not straight or road.
{
"error_id": "INVALID_VALUE",
"errors": [
{
"message": "Input should be 'straight' or 'road'",
"help": "https://pro6pp.nl/docs/v2/redoc",
"parameter": "algorithm",
"value": "fast"
}
]
}UNAUTHORIZEDThe API key is missing, unknown, revoked or expired, its subscription is not active, the request does not match the key's allowlist, or the subscription does not include this call.
The API key is missing, unknown, revoked or expired, its subscription is not active, the request does not match the key's allowlist, or the subscription does not include this call.
{
"error_id": "UNAUTHORIZED",
"errors": [
{
"message": "No authorization token provided",
"help": "https://pro6pp.nl/docs/v2/redoc#section/Authentication"
}
]
}Esempi di codice
curl "https://api.pro6pp.nl/v2/distance/ch?toPostalCode=4145&fromPostalCode=4146&algorithm=road&authKey=YOUR_API_KEY"La invitiamo a richiedere codice di esempio nel linguaggio di programmazione che preferisce.
Passo dopo passo
Seguendo i passaggi seguenti, crei un semplice modulo per provare la funzionalità Svizzera Distance.
Creare la pagina HTML
Iniziamo creando una pagina HTML vuota con una struttura web minima. Crei un nuovo file
.htmldenominatopro6pp_svizzera_distance.html. Aggiunga il seguente codice tra i tag<body>e</body>. Questo codice aggiunge i campi di input per l'inserimento dell'indirizzo.<form class="form" id="form"> <div>Call</div> <div> <label for="toPostalCode">toPostalCode:</label> <input id="toPostalCode" class="toPostalCode" placeholder="4145"/> <label for="fromPostalCode">fromPostalCode:</label> <input id="fromPostalCode" class="fromPostalCode" placeholder="4146"/> <label for="algorithm">algorithm:</label> <input id="algorithm" class="algorithm" placeholder="road"/> </div> <div> <span style="color: red" class="message"></span> </div> <div> <button type="submit">Submit</button> </div> <br /> <div>Response</div> <br /> <div>Full Response</div> <pre class="jsonResponse"></pre> </form>Aggiungere l'interazione
Aggiunga il seguente codice JavaScript in un file separato
pro6pp_svizzera_distance.js. Gli IDs e i nomi delle classi (Class) degli elementi HTML devono corrispondere a quelli indicati in questo file JavaScript per ottenere i risultati corretti.document.addEventListener("DOMContentLoaded", function() { const pro6ppAuthKey = "AUTH_KEY"; // Replace with your actual authorization key let messageSpan = document.querySelector(".message"); let jsonResponseSpan = document.querySelector(".jsonResponse"); const BASE_URL = "https://api.pro6pp.nl/v2"; function fetchAddress(authKey) { const queryParams = { toPostalCode: document.getElementById("toPostalCode").value, fromPostalCode: document.getElementById("fromPostalCode").value, authKey: authKey }; const optionalQueryParams = [ "algorithm", ]; for (const name of optionalQueryParams) { const value = document.getElementById(name).value; if (value !== "") { queryParams[name] = value; } } let encodedUrl = new URL(`${BASE_URL}/distance/ch`); encodedUrl.search = new URLSearchParams(queryParams); fetch(encodedUrl) .then(async (response) => { const data = await response.json(); if (data) { messageSpan.textContent = ""; jsonResponseSpan.textContent = JSON.stringify(data, null, 2); } }) .catch((error) => { console.error("Error fetching: ", error); messageSpan.textContent = error.message; }); } const form = document.getElementById("form"); form.onsubmit = function (event) { event.preventDefault(); fetchAddress(pro6ppAuthKey); } });Farlo funzionare
Colleghi il file JavaScript all'interno di
pro6pp_svizzera_distance.htmlsubito prima del tag di chiusura</body>.... <script src="pro6pp_svizzera_distance.js"></script> </body> </html>Per accedere al servizio web Pro6PP è necessario richiedere la Sua chiave di autorizzazione personale. La chiave Le viene inviata via e-mail subito dopo la registrazione. Sostituisca il segnaposto
AUTH_KEYindicato sopra con la Sua chiave di autorizzazione personale.Se non ha ancora un account, si registri per una prova gratuita di 2 mesi. Dopo aver creato l'account, vada in Dettagli abbonamento e copi la Sua
AUTH_KEY. Questa chiave Le servirà per autenticare le Sue richieste.Vederlo in azione
Una volta completati i passaggi precedenti, è il momento di eseguire il modulo in locale nel Suo browser. Ora dovrebbe avere due file nella stessa cartella del Suo computer:
pro6pp_svizzera_distance.html- la pagina HTML con il modulo.pro6pp_svizzera_distance.js- il file JavaScript che gestisce l'interazione.
Ora può aprire il file HTML nel Suo browser semplicemente facendo doppio clic su di esso nel gestore file (ad es. Finder su macOS o File Explorer su Windows). In alternativa, può aprirlo manualmente nel browser utilizzando il percorso completo
file:///. Ad esempio:file:///C:/Users/YourName/Documents/pro6pp_svizzera_distance.html file:///Users/yourname/Desktop/pro6pp_svizzera_distance.htmlUna volta aperta la pagina, inizi a digitare nei campi del modulo e prema Submit per visualizzare la risposta dell'API nella sezione "Full Response".