Geocoding for openstreetmap

Hi Team,

I have 2 text box one is called from address and another one is called to address. Now I have search button on click of search button i want to display the distance between from address and to address.

Example:

Suppose I have London in from address and France in to address now if i click on search button it should show the distance between these two places.

How I can do that, I know I have to do geocoding for that but I am a very novice person in geocoding.

Could you please let me know the steps or sample code for this.

Thanks in advance

Avtar

You can use the Nominatim service to do address to location lookups.

Do you need the straight distance or do you need the distance using roads and different types of transport? The first can be solved by a simple calculation using e.g. the Great Circle Distance Formula (see below) and the second needs a routing application (i.e. shortest path).

function getDistanceKm($latitudeFrom, $longitudeFrom, $latituteTo, $longitudeTo) {
    // 1 degree equals 0.017453292519943 radius
    $degreeRadius = deg2rad(1);
 
    // convert longitude and latitude values to radians before calculation
    $latitudeFrom  *= $degreeRadius;
    $longitudeFrom *= $degreeRadius;
    $latituteTo    *= $degreeRadius;
    $longitudeTo   *= $degreeRadius;
 
    // apply the Great Circle Distance Formula
    $d = sin($latitudeFrom) * sin($latituteTo) + cos($latitudeFrom)
       * cos($latituteTo) * cos($longitudeFrom - $longitudeTo);
 
    return (6371.0 * acos($d));
}