Hi friends, hope you are doing well. In today’s post i am going to share very useful API. After this post you will be know how to calculate distance between origin and destination using google map api in php.
This PHP scripts will get the distance and time estimation using google map API. Below is the google map API url Generate Google API Key.
Before proceed forward you need to create google MAP API key. Click here to generate API Key.
On the below screen click on GET A KEY button to generate API KEY.
In the below page click create and enable key button.
After the processing you will get the API Key. Your API key is generated her is an example API key YOUR API KEY :
AIzaSyAvSwwcLmQcJSHfqtMC3Tpzd3pM62tniLA .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <form method="POST"> <b>Entry Point: </b> <select name="start" id="start"> <option value="select">Select</option> <option value="chicago, il">Chicago</option> <option value="st louis, mo">St Louis</option> <option value="joplin, mo">Joplin, MO</option> <option value="oklahoma city, ok">Oklahoma City</option> <option value="amarillo, tx">Amarillo</option> <option value="gallup, nm">Gallup, NM</option> <option value="flagstaff, az">Flagstaff, AZ</option> <option value="winona, az">Winona</option> <option value="kingman, az">Kingman</option> <option value="barstow, ca">Barstow</option> <option value="san bernardino, ca">San Bernardino</option> <option value="los angeles, ca">Los Angeles</option> </select> <b>Exit Point: </b> <select name="end" id="end"> <option value="select">Select</option> <option value="chicago, il">Chicago</option> <option value="st louis, mo">St Louis</option> <option value="joplin, mo">Joplin, MO</option> <option value="oklahoma city, ok">Oklahoma City</option> <option value="amarillo, tx">Amarillo</option> <option value="gallup, nm">Gallup, NM</option> <option value="flagstaff, az">Flagstaff, AZ</option> <option value="winona, az">Winona</option> <option value="kingman, az">Kingman</option> <option value="barstow, ca">Barstow</option> <option value="san bernardino, ca">San Bernardino</option> <option value="los angeles, ca">Los Angeles</option> </select> <input type="submit" name="submit" id="submit" value="Calculate"> </form> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php /* * @Author: Prem Tiwari * Get distance and time between origin & destination */ if (isset($_POST['submit'])) { echo $url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='.urlencode($_POST['start']).'|Seattle&destinations='.urlencode($_POST['end']).'&key=AIzaSyA-zbBroyICRbc5NfrZINE7hJUrg0_aFNw'; $geocode = file_get_contents($url); $outputs = json_decode($geocode); // print distance $distance = $outputs->rows[0]->elements[0]->distance->text; // print time duration $duration = $outputs->rows[0]->elements[0]->duration->text; echo $results = '<table><tr> <th>Distance</th> <th>Estimated Time Duration</th> </tr> <tr> <td>'.$distance.'</td> <td>'.$duration.'</td> </td></tr> </table>'; } ?> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: calculate distance google maps, distance calculator google maps, google distance calculator maps, google distance map, google map api, google map distance calculator, google maps api calculate distance, google maps calculate distance, google maps distance calculator, PHP, php script to distance calculation