Hello! In this post, you will learn how to calculate distance between two places using google map API in php.Here you will calculate by air distance between two countries. See below ..how to do it.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
<?php function getDistance($addressFrom, $addressTo, $unit){ //Change address format $formattedAddrFrom = str_replace(' ','+',$addressFrom); $formattedAddrTo = str_replace(' ','+',$addressTo); //Send request and receive json data $geocodeFrom = file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$formattedAddrFrom.'&sensor=false&key=AIzaSyBOMQKyG37IA6VhextrNpB0YxQnsR6Phys'); $outputFrom = json_decode($geocodeFrom); $geocodeTo = file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$formattedAddrTo.'&sensor=false&key=AIzaSyBOMQKyG37IA6VhextrNpB0YxQnsR6Phys'); $outputTo = json_decode($geocodeTo); //print_r($outputFrom); //Get latitude and longitude from geo data $latitudeFrom = $outputFrom->results[0]->geometry->location->lat; $longitudeFrom = $outputFrom->results[0]->geometry->location->lng; $latitudeTo = $outputTo->results[0]->geometry->location->lat; $longitudeTo = $outputTo->results[0]->geometry->location->lng; //Calculate distance from latitude and longitude $theta = $longitudeFrom - $longitudeTo; $dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) + cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.151; $unit = strtoupper($unit); if ($unit == "K") { return ($miles * 1.609344).' km'; } else if ($unit == "N") { return ($miles * 0.8684).' nm'; } else { return $miles.' mi'; } } if(isset($_REQUEST['cal'])) { $addressFrom = $_POST['addressfrom']; $addressTo =$_POST['addressto']; $distance = getDistance($addressFrom, $addressTo, "K"); } ?> <html> <head> <title>Distance Calculation </title> <style> * { margin: 0; padding: 0; } .container { width: 800px; margin: 0 auto; border: 0px green dashed; } p { text-align: center; margin-bottom: 15px; padding: 5px; font-size: 16px; } input { padding: 10px; width: 500px; font-size: 16px; border: 1px #003366 solid; } textarea { width: 500px; height: 150px; resize: none; font-size: 16px; padding: 10px; border: 1px #003366 solid; } </style> <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBOMQKyG37IA6VhextrNpB0YxQnsR6Phys&libraries=places"></script> <script> var autocomplete; function initialize() { autocomplete = new google.maps.places.Autocomplete( /** @type {HTMLInputElement} */(document.getElementById('autocomplete')), { types: ['geocode'] }); google.maps.event.addListener(autocomplete, 'place_changed', function() { }); autocomplete = new google.maps.places.Autocomplete( /** @type {HTMLInputElement} */(document.getElementById('autocomplete1')), { types: ['geocode'] }); google.maps.event.addListener(autocomplete1, 'place_changed', function() { }); } </script> </head> <body onload="initialize()"> <div class="container" id="locationField"> <center> <h1>Distance: <?php if(isset($distance)) { echo $distance; } ?></h1> </center> <form action="" method="post"> <p> <input type="text" required placeholder="From" name="addressfrom" id="autocomplete" onFocus="geolocate()" /> </p> <p> <input type="text" required placeholder="To" name="addressto" id="autocomplete1" onFocus="geolocate()" /> </p> <p> <input type="submit" name="cal" value="Calculate"> </p> </form> </div> </body> </html> |