In this article, you will learn about How to Draw Route Path between two locations on Map using Google Maps Direction API in PHP. You can easily display maps on the website using Google Maps API and point the location using…
Expand +Category: Google API
Convert text from one language to another using Google Translate API in PHP / JavaScript
In this article, I have explained how to translate text from one language to another using Google Translate API in PHP / JavaScript. The daily limit of the Translate API is 2 million characters per day. You can increase the…
Expand +How to Get Current Visitor Location using HTML5 Geolocation API
In this article, we will learn about How to Get Current Visitor Location using HTML5 Geolocation API in PHP. The geolocation feature in HTML5 enables you to determine the geographical coordinates of the current visitor location of your website (latitude…
Expand +How to Add or Embed Google Review on your website
Embed or display google customer review on your website is an easy way to create a market trust in front of your customer or visitor. Currently, every company have google business profile to promote your business in google and display…
Expand +How to create PHP Text Captcha without google captcha API
PHP Text Captcha is a image of text which is generated by free captcha script. It is used to protect your login form, register form or any forms from spam ,abuse and hackers. In this article, we will learn How…
Expand +How to move animated marker smoothly using Google Map Javascript API
In this article, you will learn about how to create moving Google map marker smoothly using Google maps JavaScript API. You can easily display maps on the website using Google Maps API and point the location using marker on map and…
Expand +How to create Google Maps Javascript API Step by Step
Hello guys, Today i going to share an article “How to create Google Maps JavaScript API” with you. Here you will learn to create JavaScript API for google Maps. As you know, The Google Maps API provides an easiest way…
Expand +How to Capture website Screenshot through website URL using Google PageSpeed Insights API and PHP
In this article you will learn “How to Capture website Screenshot through website URL using Google PageSpeed Insights API and PHP” Usually, Google PageSpeed Insight API is used to check the performance and speed of any website or webpage. But…
Expand +How to create Youtube Search Engine using jQuery
In this article, we are going to learn “How to create Youtube Search Engine using jQuery“. It is a easy jQuery script which help you to create Youtube Search Engine. you can use this in your website to search video…
Expand +Distance calculator between two places using google map API in php
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> |