{"id":642,"date":"2015-06-05T15:56:02","date_gmt":"2015-06-05T15:56:02","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=642"},"modified":"2024-06-25T07:09:38","modified_gmt":"2024-06-25T07:09:38","slug":"distance-between-two-addresses-google-maps-api-php","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/","title":{"rendered":"Calculate Distance Between Two Addresses using Google Maps API and PHP"},"content":{"rendered":"<p>The geocoding process is used to convert the address into geographic coordinates. <b>Google Maps Geocoding API<\/b> provides an easy way to fetch geographic data from addresses. Generally, Geocoding API is used to get the latitude and longitude of an address in the web application. Apart from this, Google Maps Geocoding API can be used for various purposes related to the Geocoding data.<\/p>\n<p>The distance calculation is useful when your web application works with the user&#8217;s location. You can easily calculate the <b>distance between addresses using Google Maps API<\/b> and PHP. In this tutorial, we will show you how to <b>calculate distance between two addresses<\/b> with Google Maps Geocoding API using PHP.<\/p>\n<h2>Google Maps API Key<\/h2>\n<p>The Google Maps API Key is required to post HTTP request to Geocoding API and get response. Before getting started with the integration process, create an API key on the Google Cloud console.<\/p>\n<ul>\n<li>Go to the <a href=\"https:\/\/console.cloud.google.com\/\" target=\"_blank\" rel=\"noopener\">Google Cloud console<\/a>.<\/li>\n<li>Select the project from the Project drop-down menu at the top. If you don&#8217;t have an existing project, create a new one.<\/li>\n<li>In the left navigation pane, select the <b>APIs &amp; Services<\/b> &raquo; <b>Credentials<\/b>.<\/li>\n<li>In the Credentials page, select <b>CREATE CREDENTIALS<\/b> &raquo; <b>API key<\/b>.<\/li>\n<li>The API key will be created and a dialog will appear with the newly created API key.<\/li>\n<li>Navigate to the Library page from the left menu panel and make sure the <b>Geocoding API<\/b> is enabled.<\/li>\n<\/ul>\n<p>Copy this API key for later use in the script on the Google Maps Geocoding API request.<\/p>\n<h2>Calculate Distance Between Addresses using PHP<\/h2>\n<p>The following example code shows the steps to get the distance between two addresses using Google Geocoding API and PHP.<\/p>\n<ul>\n<li>Set the required values in the defined variable.\n<ul>\n<li><code>$GOOGLE_API_KEY<\/code> &#8211; Specify the Google API Key.<\/li>\n<li><code>$addressFrom<\/code> &#8211; Specify the address where the distance calculation will start from.<\/li>\n<li><code>$addressTo<\/code> &#8211; Specify the address where the distance calculation will end.<\/li>\n<\/ul>\n<\/li>\n<li>Format the address string by replacing the empty spaces with plus (+) sign.<\/li>\n<li>Request Google Geocoding API (<code>https:\/\/maps.googleapis.com\/maps\/api\/geocode\/json<\/code>) using the file_get_contents() method in PHP.\n<ul>\n<li>Execute Geocoding API requests for both, start and end addresses to get geographic data.<\/li>\n<li>Pass the <code>address<\/code> (from which you want to calculate distance) and <code>key<\/code> parameters in the Geocoding API URL as a query string.<\/li>\n<\/ul>\n<\/li>\n<li>Decode JSON response returned by the Geocoding API using PHP json_decode() function.<\/li>\n<li>Retrieve latitude and longitude from the <code>geometry<\/code> object of API response.<\/li>\n<li>Calculate distance using the latitude and longitude of start and end addresses.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Google&nbsp;Maps&nbsp;API&nbsp;Key <br \/><\/span><span style=\"color: #0000BB\">$GOOGLE_API_KEY&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'YOUR_API_KEY_HERE'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Addresses&nbsp;between&nbsp;which&nbsp;distances&nbsp;will&nbsp;be&nbsp;calculated <br \/><\/span><span style=\"color: #0000BB\">$addressFrom&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'616&nbsp;Scholes&nbsp;St,&nbsp;Brooklyn,&nbsp;NY&nbsp;11237'<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/start&nbsp;address <br \/><\/span><span style=\"color: #0000BB\">$addressTo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'6503&nbsp;Metropolitan&nbsp;Ave,&nbsp;Queens,&nbsp;NY&nbsp;11379'<\/span><span style=\"color: #007700\">;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/end&nbsp;address <br \/> <br \/>\/\/&nbsp;Format&nbsp;address&nbsp;string <br \/><\/span><span style=\"color: #0000BB\">$formatted_address_from&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">str_replace<\/span><span style=\"color: #007700\">(array(<\/span><span style=\"color: #DD0000\">'&nbsp;'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'&amp;'<\/span><span style=\"color: #007700\">),&nbsp;<\/span><span style=\"color: #DD0000\">'+'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$addressFrom<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$formatted_address_to&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">str_replace<\/span><span style=\"color: #007700\">(array(<\/span><span style=\"color: #DD0000\">'&nbsp;'<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #DD0000\">'&amp;'<\/span><span style=\"color: #007700\">),&nbsp;<\/span><span style=\"color: #DD0000\">'+'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$addressTo<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Geocoding&nbsp;API&nbsp;request&nbsp;with&nbsp;start&nbsp;address <br \/><\/span><span style=\"color: #0000BB\">$geocode_data_start&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">file_get_contents<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"https:\/\/maps.googleapis.com\/maps\/api\/geocode\/json?address=<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$formatted_address_from<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">&amp;key=<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$GOOGLE_API_KEY<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\"<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$outputFrom&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">json_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$geocode_data_start<\/span><span style=\"color: #007700\">); <br \/>if(!empty(<\/span><span style=\"color: #0000BB\">$outputFrom<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error_message<\/span><span style=\"color: #007700\">)){ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;die(<\/span><span style=\"color: #DD0000\">\"API&nbsp;Error:&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$outputFrom<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error_message<\/span><span style=\"color: #007700\">); <br \/>}elseif(empty(<\/span><span style=\"color: #0000BB\">$outputFrom<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">results<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">])){ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;die(<\/span><span style=\"color: #DD0000\">\"Returns&nbsp;empty&nbsp;geodata:&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$addressFrom<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Geocoding&nbsp;API&nbsp;request&nbsp;with&nbsp;end&nbsp;address <br \/><\/span><span style=\"color: #0000BB\">$geocode_data_end&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">file_get_contents<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"https:\/\/maps.googleapis.com\/maps\/api\/geocode\/json?address=<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$formatted_address_to<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">&amp;key=<\/span><span style=\"color: #007700\">{<\/span><span style=\"color: #0000BB\">$GOOGLE_API_KEY<\/span><span style=\"color: #007700\">}<\/span><span style=\"color: #DD0000\">\"<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$outputTo&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">json_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$geocode_data_end<\/span><span style=\"color: #007700\">); <br \/>if(!empty(<\/span><span style=\"color: #0000BB\">$outputTo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error_message<\/span><span style=\"color: #007700\">)){ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;die(<\/span><span style=\"color: #DD0000\">\"API&nbsp;Error:&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$outputTo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error_message<\/span><span style=\"color: #007700\">); <br \/>}elseif(empty(<\/span><span style=\"color: #0000BB\">$outputTo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">results<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">])){ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;die(<\/span><span style=\"color: #DD0000\">\"Returns&nbsp;empty&nbsp;geodata:&nbsp;\"<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$addressTo<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Retrieve&nbsp;latitude&nbsp;and&nbsp;longitude&nbsp;from&nbsp;the&nbsp;geodata <br \/><\/span><span style=\"color: #0000BB\">$latitude_from&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$outputFrom<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">results<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">]-&gt;<\/span><span style=\"color: #0000BB\">geometry<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lat<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$longitude_from&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$outputFrom<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">results<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">]-&gt;<\/span><span style=\"color: #0000BB\">geometry<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lng<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$latitude_to&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$outputTo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">results<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">]-&gt;<\/span><span style=\"color: #0000BB\">geometry<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lat<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$longitude_to&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$outputTo<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">results<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">]-&gt;<\/span><span style=\"color: #0000BB\">geometry<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">lng<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Calculate&nbsp;distance&nbsp;between&nbsp;latitudes&nbsp;and&nbsp;longitudes <br \/><\/span><span style=\"color: #0000BB\">$theta&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$longitude_from&nbsp;<\/span><span style=\"color: #007700\">-&nbsp;<\/span><span style=\"color: #0000BB\">$longitude_to<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$dist&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">sin<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">deg2rad<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$latitude_from<\/span><span style=\"color: #007700\">))&nbsp;*&nbsp;<\/span><span style=\"color: #0000BB\">sin<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">deg2rad<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$latitude_to<\/span><span style=\"color: #007700\">))&nbsp;+&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">cos<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">deg2rad<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$latitude_from<\/span><span style=\"color: #007700\">))&nbsp;*&nbsp;<\/span><span style=\"color: #0000BB\">cos<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">deg2rad<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$latitude_to<\/span><span style=\"color: #007700\">))&nbsp;*&nbsp;<\/span><span style=\"color: #0000BB\">cos<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">deg2rad<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$theta<\/span><span style=\"color: #007700\">)); <br \/><\/span><span style=\"color: #0000BB\">$dist&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">acos<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$dist<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$dist&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">rad2deg<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$dist<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$miles&nbsp;&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$dist&nbsp;<\/span><span style=\"color: #007700\">*&nbsp;<\/span><span style=\"color: #0000BB\">60&nbsp;<\/span><span style=\"color: #007700\">*&nbsp;<\/span><span style=\"color: #0000BB\">1.1515<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Get&nbsp;distance&nbsp;in&nbsp;miles <br \/><\/span><span style=\"color: #0000BB\">$distance&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">round<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$miles<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">).<\/span><span style=\"color: #DD0000\">'&nbsp;miles'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<p>Display the distance two addresses on the web page.<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Render&nbsp;the&nbsp;distance&nbsp;between&nbsp;the&nbsp;given&nbsp;addresses <br \/><\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;p&gt;Distance:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$distance<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;\/p&gt;'<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p>You can convert units and get the distance in a human-readable format (such as km, meter, etc.) using PHP:<\/p>\n<pre><span style=\"color: #FF8000\">\/\/&nbsp;Distance&nbsp;in&nbsp;kilometer <br \/><\/span><span style=\"color: #0000BB\">$distance_km&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">round<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$miles&nbsp;<\/span><span style=\"color: #007700\">*&nbsp;<\/span><span style=\"color: #0000BB\">1.609344<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">).<\/span><span style=\"color: #DD0000\">'&nbsp;km'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Distance&nbsp;in&nbsp;meter <br \/><\/span><span style=\"color: #0000BB\">$distance_meter&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">round<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$miles&nbsp;<\/span><span style=\"color: #007700\">*&nbsp;<\/span><span style=\"color: #0000BB\">1609.344<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">).<\/span><span style=\"color: #DD0000\">'&nbsp;meters'<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p class=\"seeAlso\"><span><\/span><a href=\"https:\/\/www.codexworld.com\/get-zipcode-from-address-using-google-maps-api-php\/\">Get zipcode from address using Google Maps API and PHP<\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>This simple distance calculator script will be very useful for getting the distance from addresses with PHP. You can easily calculate distance between two locations or coordinates using PHP. There are various geographic info will be available in Geocoding API&#8217;s response and you can use them for other purposes as needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The geocoding process is used to convert the address into geographic coordinates. Google Maps Geocoding API provides an easy way to fetch geographic data from addresses. Generally, Geocoding API is used to get the latitude <\/p>\n","protected":false},"author":1,"featured_media":5672,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[250,380,13,15,35,14],"class_list":["post-642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-api","tag-geocoding","tag-google-api","tag-google-maps","tag-google-maps-api","tag-php","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Calculate Distance Between Two Addresses using Google Maps API and PHP - CodexWorld<\/title>\n<meta name=\"description\" content=\"Calculate the distance between two addresses using Google Maps Geocoding API and PHP - Get distance between two addresses through PHP. Example code to calculate distance using PHP and Google Map API.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Calculate Distance Between Two Addresses using Google Maps API and PHP - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Calculate the distance between two addresses using Google Maps Geocoding API and PHP - Get distance between two addresses through PHP. Example code to calculate distance using PHP and Google Map API.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-05T15:56:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-25T07:09:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Calculate Distance Between Two Addresses using Google Maps API and PHP\",\"datePublished\":\"2015-06-05T15:56:02+00:00\",\"dateModified\":\"2024-06-25T07:09:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/\"},\"wordCount\":508,\"commentCount\":20,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2015\\\/06\\\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png\",\"keywords\":[\"API\",\"Geocoding\",\"Google API\",\"Google Maps\",\"Google Maps API\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/\",\"name\":\"Calculate Distance Between Two Addresses using Google Maps API and PHP - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2015\\\/06\\\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png\",\"datePublished\":\"2015-06-05T15:56:02+00:00\",\"dateModified\":\"2024-06-25T07:09:38+00:00\",\"description\":\"Calculate the distance between two addresses using Google Maps Geocoding API and PHP - Get distance between two addresses through PHP. Example code to calculate distance using PHP and Google Map API.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2015\\\/06\\\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2015\\\/06\\\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png\",\"width\":1920,\"height\":1080,\"caption\":\"calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/distance-between-two-addresses-google-maps-api-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Calculate Distance Between Two Addresses using Google Maps API and PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Calculate Distance Between Two Addresses using Google Maps API and PHP - CodexWorld","description":"Calculate the distance between two addresses using Google Maps Geocoding API and PHP - Get distance between two addresses through PHP. Example code to calculate distance using PHP and Google Map API.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/","og_locale":"en_US","og_type":"article","og_title":"Calculate Distance Between Two Addresses using Google Maps API and PHP - CodexWorld","og_description":"Calculate the distance between two addresses using Google Maps Geocoding API and PHP - Get distance between two addresses through PHP. Example code to calculate distance using PHP and Google Map API.","og_url":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2015-06-05T15:56:02+00:00","article_modified_time":"2024-06-25T07:09:38+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png","type":"image\/png"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Calculate Distance Between Two Addresses using Google Maps API and PHP","datePublished":"2015-06-05T15:56:02+00:00","dateModified":"2024-06-25T07:09:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/"},"wordCount":508,"commentCount":20,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png","keywords":["API","Geocoding","Google API","Google Maps","Google Maps API","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/","url":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/","name":"Calculate Distance Between Two Addresses using Google Maps API and PHP - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png","datePublished":"2015-06-05T15:56:02+00:00","dateModified":"2024-06-25T07:09:38+00:00","description":"Calculate the distance between two addresses using Google Maps Geocoding API and PHP - Get distance between two addresses through PHP. Example code to calculate distance using PHP and Google Map API.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png","width":1920,"height":1080,"caption":"calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/distance-between-two-addresses-google-maps-api-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Calculate Distance Between Two Addresses using Google Maps API and PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2015\/06\/calculate-distance-between-two-addresses-google-maps-geocoding-api-php-codexworld.png","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-am","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":12,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":5671,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/642\/revisions\/5671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/5672"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}