Calculating driving distance between places

Viewed 940

I managed to get an autocomplete function to a form input with Maptiler API (OSMNames) (since I find Google Maps API even more confusing).

So here comes the actual problem, I would like to get the driving distance between two places I entered with the help of the autocomplete functions in the two inputs I have created.

I am trying to do so with the OSRM (Open Source Routing Machine) API

Here are my current scripts:

<!--AUTOCOMPLETE-->
<script>
    var autocomplete = new kt.OsmNamesAutocomplete(
    'search1', 'https://geocoder.tilehosting.com/', '#My_Maptiler_API_Code');
</script>
<script>
    var autocomplete = new kt.OsmNamesAutocomplete(
    'search2', 'https://geocoder.tilehosting.com/', '#My_Maptiler_API_Code');
</script>

<!--GET INFO-->
<script>
    function loadXMLDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("result").innerHTML =
            this.responseText;
        }

        if (this.readyState == 4 && this.status == 429) {
            document.getElementById("result").innerHTML =
            "Servidor sobrecargado, inténtelo más tarde."
        }
    };
    xhttp.open("GET", "http://router.project-osrm.org/table/v1/driving/{longitude1},{latitude1};{longitude2},{latitude2}", true);
    xhttp.send();
    }
</script>

First, I need to get the coordinates of the places from the inputs with the help of the autocomplete function.

Currently, when the user clicks on one of the suggested places on the list, just the name item gets written in the input box. Instead, I would like to get the GPS coordinates written, not the name, so that I can use them to send the HTTP request (next step).

What is being written when I click:

What is being written when I click

What I need for the next step:

What I need for the next step

Btw, I have seen some answers in other related questions inviting to use "Nominatim". As I said, I am currently using "OSMNames", is there any difference? Should I change my geocoder?

Then, I would also need to get these coordinates in the HTTP GET request, as shown on the code.

And finally, I would like to get just one of the items from the OSRM response, the distance. Since otherwise, I would just get the entire response.

I have literally no idea on how to do any of the stated steps since I am a student that is still getting started on this area.

If only you could lend me a hand here it would be excellent.

I try to avoid putting all the code here because I find it confusing when reading but should you need the rest of the code for any reason, please let me know!

Thanks for your help! :)

0 Answers
Related