Solved

Calculate distance by latitude and longitude

Posted on 2014-10-25
5
179 Views
Last Modified: 2014-11-07
I'm using this php function but I'm having a hard time to test if it works.

Basically what I would like to do is to use this Google API request:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=location=".$jsonLAT.",".$jsonLNG."&radius=3000

Therefore i need to use the location paramater "location — The latitude/longitude around which to retrieve place information. " When I echo $jsonLAT or jsonLNG there will be no result.



		function distance($lat1,$lon1,$lat2,$lon2) {
		    $R = 6371; // km (change this constant to get miles)
		    $dLat = ($lat2-$lat1) * pi() / 180;
		    $dLon = ($lon2-$lon1) * pi() / 180;
		    $a = sin($dLat/2) * sin($dLat/2) +
		        cos($lat1 * pi() / 180 ) * cos($lat2 * pi() / 180 ) *
		        sin($dLon/2) * sin($dLon/2);
		    $c = 2 * atan2(sqrt($a), sqrt(1-$a));
		    $d = $R * $c;
		    if ($d>1) {return round($d) * 1000;
		     } else if ($d<=1){ return round($d*1000);
			} else { 
		    return $d;
			}
		}
		//print_r($thisStuff);
		$address = $thisStuff['bizAddr'].'+'.$thisStuff['bizCity'].'+'.$thisStuff['bizState'].'+'.$thisStuff['bizZip'];
		$address .= '+USA';
		$address = str_replace(" ", "+", $address);
		$json_data = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false");
		$json_data = json_decode($poiAddress);
		$poiAddress = $json_data->{'results'}[0]->{'geometry'};
		
		$jsonLAT = $poiAddress->{'location'}->{'lat'};
		$jsonLNG = $poiAddress->{'location'}->{'lng'};
		$searchRadius = 3000;
		$lat1 = $poiAddress->{'bounds'}->{'northeast'}->{'lat'}; $lon1 = $poiAddress->{'bounds'}->{'northeast'}->{'lng'}; $lat2 = $poiAddress->{'bounds'}->{'southwest'}->{'lat'}; $lon2 = $poiAddress->{'bounds'}->{'southwest'}->{'lng'};
		$searchRadius = distance($lat1,$lon1,$lat2,$lon2);
		

Open in new window

0
Comment
Question by:seopti
  • 3
  • 2
5 Comments
 

Author Comment

by:seopti
ID: 40404239
Spent countless hours on this and found out that this request won't work:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=location=".$jsonLAT.",".$jsonLNG."&radius=3000&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw

But this one will work when I manually enter lat + lng instead of the variable:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=36.418367,-89.057683&radius=3000&key=AIzaSyBSCZt0IMSPZjkoyiEIj08953OBI3qVZBw

So this means the problem is with the variables $jsonLAT and json LNG. You can copy + paste the second request and you will see the JSON response. But I still can't find out why $jsonLAT and jonLNG won't work.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40404241
It looks like you're using the Haversine algorithm.  If the distance is less than about 100 miles, plane geometry seems to work well, too.  Beyond that, Haversine improves accuracy.  Neither takes into account things that are issues at very short distances, such as inaccurate start/end points (ZIP codes, common in these calculations, are not locations, but are instead postal carriers' routes) or natural barriers such as mountains and rivers.

This article shows how I have used the Haversine formula.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_4276-What-is-near-me-Proximity-calculations-using-PHP-and-MySQL.html

This article shows how I have used the Google Map Class.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3350-Using-the-Google-Maps-API-in-PHP.html

If you want to post some test data (the most important thing a programmer can have), I'll be glad to try to show you how I might implement your distance calculations.
0
 

Author Comment

by:seopti
ID: 40404244
Also the geocoding works fine, for example:
http://maps.googleapis.com/maps/api/geocode/json?address=231+Public+Sq+Nashville+Tennessee+37201+USA&sensor=false

So the problem must be somewhere with these lines of code:

		$json_data = json_decode($poiAddress);
		$poiAddress = $json_data->{'results'}[0]->{'geometry'};
		
		$jsonLAT = $poiAddress->{'location'}->{'lat'};
		$jsonLNG = $poiAddress->{'location'}->{'lng'};
		$searchRadius = 3000;
		$lat1 = $poiAddress->{'bounds'}->{'northeast'}->{'lat'}; $lon1 = $poiAddress->{'bounds'}->{'northeast'}->{'lng'}; $lat2 = $poiAddress->{'bounds'}->{'southwest'}->{'lat'}; $lon2 = $poiAddress->{'bounds'}->{'southwest'}->{'lng'};
		$searchRadius = distance($lat1,$lon1,$lat2,$lon2);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40404251
Please see: http://iconoun.com/demo/temp_seopti.php

Here is the script.  I copied the JSON string from the output of the Google APIs URL that worked.  What comes next?
<?php // demo/temp_seopti.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28544554.html

$jso = <<<EOD
{
   "html_attributions" : [],
   "next_page_token" : "CvQB7AAAAJzRf-BH7zNIsPRrE_9yj3AH-24HirikzBx3gQH0hQ9hyVQvh6BMJwy5qm12BiR1BBBWC3ktR34VdhzuVRHn-sXjYqIzBf8cauF_AJtzYegYtiLGAbnI8iXZqzftd9GnbEJoEEyYplcUjhKR15aUq4HxKQh3thVHt9R-2UkOxned93fs8wBhJJorsspRaCxD7Z3oti23CXQcXGAn9cTSCGq4yw4y5pEeV1AIf9y9hiC3LhL-xLXAhMJPPenZi4qq80J9rZUcXmSlzxsMVwr8uqgRicZxCnf3At8bOixwUUOH4A9Otkw21wypCXQo_vsjPRIQoVi0QJEppxMTS-akCnV7DBoUGqlj64dPalBHYLOC6fUBkfC2_MY",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 36.4242303,
               "lng" : -89.0570096
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 36.465505,
                  "lng" : -88.999202
               },
               "southwest" : {
                  "lat" : 36.39093,
                  "lng" : -89.099436
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "98ba9c3639534d44daef16c09722f0afeeacb2b7",
         "name" : "Union City",
         "place_id" : "ChIJe8fmOsoReYgRB68Xo4_Qx0U",
         "reference" : "CpQBhQAAAPYbWwTi2dK4oReunkE-zIwvLJ8_DjoaePYeSKRgiY6xQRNHmWmbdbBXpHrstSSLNXgKF1-cMEry9qX8ce8CRM6NSvfNBrfVb6baTVjHe6PnL-ZEtYo9VOim_qr4u-9OyaHKmUHlMXEj7sW86gvgLC2ftKCWLZ2wxd7dMNAsCPWeCu5TKjhW_Ez77F4m9Sxp6xIQ6mrEC2PoRtm1UxM3MOusohoUH_DOV4zA_bpjJZ2SG1ODR-kkvUo",
         "scope" : "GOOGLE",
         "types" : [ "locality", "political" ],
         "vicinity" : "Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.438128,
               "lng" : -89.070954
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/museum-71.png",
         "id" : "66f6e610461a62f934e2b458b361d90f5a8a64cd",
         "name" : "Discovery Park of America",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 480,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRoAAAAfrakakPa57fdcx3EJg7MVwMPnefJeNzi1sq5YZhQlS-1wOk67vOxrmmdD7aIEtH6yk5rnSH-Dq-Pp9Xnip6Rpjs7rUHenC32W0WGAq77AJ_L0CMPFo3KpTl2nmMllkjIEL09Z2Ld2debi-22dA_MIBIQsLl-nea2ypgCRdPj1L0sxBoUp7plMkg5Uyw0hzueJlsbi1Bvjss",
               "width" : 334
            }
         ],
         "place_id" : "ChIJ61ckLYNyeYgRLi46eFzziN4",
         "rating" : 4.3,
         "reference" : "CoQBfAAAAHsBQJ8mIxJXLL6geW8U96IUGauIAB8W3NUI4N3na_VRoehrk9WGY4pS3wx9m8bT6SpGZeFAoc4VjyAEIHk_Yu6J9GkbyeJqUxravYHJCaEYnJ_rrTRG5ChSy7RFX8fYYTIVbA3AGYDdYjhLWK0UMw3cB0RyvVa5osXSDDHD3mySEhAFbDZbYDSMUf6lUQ4VikdeGhTTqsifkzb79SJLRjssL-Ljfe1wAg",
         "scope" : "GOOGLE",
         "types" : [ "museum", "establishment" ],
         "vicinity" : "830 everett Blvd, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.427747,
               "lng" : -89.03466400000001
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "e29dffc00f42901e2472cf23182ada853bf271b3",
         "name" : "Wenk",
         "opening_hours" : {
            "open_now" : false
         },
         "photos" : [
            {
               "height" : 480,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRoAAAAeGE_-waoU88Gz_fhD8KQjTvfCkC9SDDOgMTt-h5MfyIlP9Ff0-F7OjAJ-1WJIuR9JbSoqwmVhdc0xpQqyJomcYujZj8ddijlzuHpUnl2QKgdywyMFo1I782MdEfHrIfd941hBFuj8o7HaJWYs-V8GxIQtligi0UPBn0pxESEHFvldRoUszcvF6Nvjlh92AlTYAPzUcnOadM",
               "width" : 640
            }
         ],
         "place_id" : "ChIJZ0-AgvRteYgREO_9EMRSBhQ",
         "reference" : "CnRmAAAAKIqKIgQWawMN1tDJC8C_e02FeVxBP3Q2tB69qJmXcjbXBlAr-XW_mnmMJqbrdRria7TNs-vhZqGdbpkShNgALugHlUthb-NCjZc669XpUqifg_JSbrsMbF9qnDj621dDUxZJlcaHPTUzvXiLxCboGRIQUEawM7UiS_FnTcp5qGIirBoUJACC1sCbCS0xPpDuqPgam0R071E",
         "scope" : "GOOGLE",
         "types" : [ "establishment" ],
         "vicinity" : "1729 Nailling Dr, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.403734,
               "lng" : -89.08901899999999
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
         "id" : "00a0f6f9b9eed15901486d4989cc30b2a19cd828",
         "name" : "Hampton Inn Union City",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 303,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAAdtC6HHRmJcX1WsnZYHfbrLOv_pcXkP30hEzM13T3zx4QGlsO9BX1cjs-nrsCS9XyhyNkkp8bHaATf_8Q62PD4s8ktRRXD-QRTRmXM33ITm38RkuOeHvQTVvnPVaxpT1b53Rv1feFbaVk_j9bjRFCHRIQesrTkwkgH6zALLrthlYk4BoUs5PTZlLPJswO-nW4O38U7CJumzA",
               "width" : 425
            }
         ],
         "place_id" : "ChIJ2yKJejFteYgRPZHdoNtRhDk",
         "rating" : 3.9,
         "reference" : "CoQBeAAAAHbz667PVenezdKepLEeHu1aP5UDjAsh2OvQI3FFbFRbE46_zU-n7lt2NA6AiYBCZLjZ5-_QTn3JGA-zaYyewnVTAWHhIf28I074pQ-XPUAUsWZ_nxYQlBzdVpKqhjsvHjXqsADjt3hlfIDQDh25EyHVhNoYFiUXMFS1wgjnDbSXEhDRTs-t4f8C1q4Df1SZK8pZGhQD6yNl42HRLGZS74pjDEpo0i56EQ",
         "scope" : "GOOGLE",
         "types" : [ "lodging", "establishment" ],
         "vicinity" : "2201 W Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.414819,
               "lng" : -89.05652499999999
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "9c68cabaab80dd455d243c850c8dfe7cd46b1e72",
         "name" : "SUBWAY® Restaurants",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 1600,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAAfn5NcAYI5_0GthBSNpEF6f4-5qaI2ZKBdUNfkUP_rJkPwdMT6EhVCgw8vh-bqzB-mdHq9ZesZjgujK109QSW0zjQbBV1NZK5ELMVquxU3XLOweo6E8UpXhFIq4atAs7utbPonPB6xxa6nXLG_WeRXhIQP4v4aM1pDxm8cvbC0kEyvBoUcBiooyzMTbRWIxFx8Xn7Kpo-eaQ",
               "width" : 2400
            }
         ],
         "place_id" : "ChIJe97CinVteYgRS1ixQBoKoic",
         "price_level" : 1,
         "reference" : "CoQBdgAAAOmMN06_2vvMR6q92caa-CSV_FKiwPPB9R6XCzcHWCKKFIfOtRWEDxnZo8izuRJeUFrIy1r9-Baj8XSD1SGzAvuCYIEk4fsf2GmTxWBQoUzVU8Px5fqcAyWxfU3-NDb_YXtXzBiK0w9-lN4_NPlfNymJ9h4z_3SAlYRigPiC7rhwEhD7_OICqAsz-p28XiVlDLAPGhS9tl7QiWTO-cS8CCoilBtma1A7nA",
         "scope" : "GOOGLE",
         "types" : [ "store", "meal_takeaway", "restaurant", "food", "establishment" ],
         "vicinity" : "206 E Reelfoot Ave #1, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.414384,
               "lng" : -89.05076699999999
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
         "id" : "b074d523d170e85c9699819c73788c5c5195a7b8",
         "name" : "Walgreens Store Union City",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 960,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRoAAAA-bUnh9rTOuWrRkKu1z6AK_5ALpUzNPcdL4WszQ_aeSx1I2SPYl8AF-QiYeMCgTvrgbi1DSAV14-D9FRoCCI-EyS3TdNI2tjlZioSIMCZx8KBwFMw5r_GTkddb4UD6tm8YHKm4MR9XPc3xH4MKvSazhIQJUpPCnmZehaajAMZBWLljRoUBfJFH9r-QqYkTQLngUKzJmftvY4",
               "width" : 1280
            }
         ],
         "place_id" : "ChIJUWqrOplteYgRZOpwzjn3JVQ",
         "price_level" : 2,
         "reference" : "CoQBfAAAABNuhdtZAjPPvF_RwM5i62W7ZU5Pb0ZFKaoPpW7W4x01SlraJu6-IE426quWW68gvduG32x-yWHat4ESwb4CDrVVy-_5Iu0rE4_AITsTthcjaM13pT-9_eKZXKNe9LYupeCz0SK530at3c5jlELPrSEkanIkjY2rpyg3EsxoVVH0EhD2j-fTGHqgMFzzA9Hmt7h6GhQzaZotOUL_K1SP_JkAblTEgYE3_Q",
         "scope" : "GOOGLE",
         "types" : [
            "convenience_store",
            "food",
            "pharmacy",
            "store",
            "health",
            "establishment"
         ],
         "vicinity" : "700 E Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.413424,
               "lng" : -89.074809
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/museum-71.png",
         "id" : "20d81043dd1ef833e172a1642dd2745a42bbc1d0",
         "name" : "Dixie Gun Works",
         "place_id" : "ChIJU21VhGpteYgRC7uZvMBhE_A",
         "rating" : 4,
         "reference" : "CoQBcgAAAAYFCbKsQYsQVMquOQyOwPyAeujSJ67tPfMC1h8D0d6Gh22Q17IgoYIB2aG_GcoOL-N2_LrFavfpr_5Ta1b8RtS6Xtxg370vE-1Q342xLeDVaEEsOF6lQgITmZspyTEkwZJdOR253D91n9yQ9RYqqKQ1Uo3QU5ecLr5S0w0Bf8PhEhCHFJGNmgKWFk_QBsy7KQg8GhTKwazjXM67uWM0X_sv76y2o9vbLA",
         "scope" : "GOOGLE",
         "types" : [ "store", "museum", "establishment" ],
         "vicinity" : "1412 W Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.408714,
               "lng" : -89.08285100000001
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
         "id" : "51d0cf3267c811fd8180f63673fc897d756c28c5",
         "name" : "Country Hearth Union City",
         "place_id" : "ChIJ_14CUzhteYgRB7LB2aiGy4k",
         "rating" : 3.4,
         "reference" : "CoQBfAAAAFq1pr3cS4A815w5S4SpP1FXjMsjJWtD-L5vGgLjvswHxzYDrM5gYvwg1zWIaL7wOouggBNJ0-l8kUT_opIyi5SArVS2zUEhQ6k-2ZD6qJMM849bSi5d-J8SwW4eYnFrMJDLu70_luzR5d2y1Nzhajtiv8Bvx6gM9HCTF2paj3ywEhBf67vIUulDM4ziQekc-zegGhTA1z-WBgZTqDq8IdD40BoR8Kc8cQ",
         "scope" : "GOOGLE",
         "types" : [ "lodging", "establishment" ],
         "vicinity" : "2009 W Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.436862,
               "lng" : -89.065836
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/worship_general-71.png",
         "id" : "52267f71c9d57a5a179ff712e133436a00c88580",
         "name" : "Second Baptist Church",
         "place_id" : "ChIJ61ckLYNyeYgRPK4b1Wuc-30",
         "reference" : "CoQBdwAAACzAPyh4I1GouNLQ6UzIlLsPxbKoOScpiZbF7Cym9CWu-PkP84Bh0hD51U0NWAO33ZV0uQ73YT5-bTPqUhKH6SLFVe2umZsrzGejemGULni5FJXPtG9SfzFQjohLlAidGFB789_zuUxCIIKORABaex0uqrRgP1iKA5teoH7jCRiMEhDhG5X-U-CG3mhpZUoFQmbLGhQ5ZMEab2Azo9EWysb2yAqEKTKr-Q",
         "scope" : "GOOGLE",
         "types" : [ "church", "place_of_worship", "establishment" ],
         "vicinity" : "831 Everett Blvd, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.428014,
               "lng" : -89.07574700000001
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "90695c3c2039da0ccf029cb997e89e6b1d9177e0",
         "name" : "Abernathy Harley-Davidson",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 540,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRjAAAALsimLDtzT3Lm8DS2ugXIye6c0Lhy6sdjXJ0jKGYvYyACfIXZq-x-mGlQMJQsc-yTYaKgP6Ag3cIfWX_azVh7_Kg3v0vfWHEQ5QjP-njo1xoo_dRgbKGiIvVfqow9y0x06paSw41N-Isn-Sl2LFroQBIQsIV5dmVGpEzgAvyJJ4LS-xoUhJzwpJlcXf7Dw_72BJv4g74ETuk",
               "width" : 960
            }
         ],
         "place_id" : "ChIJv2aUBV5teYgRVH8pHCMU4ss",
         "rating" : 5,
         "reference" : "CoQBfAAAAGy21Hdqxls8O6kwTRgR0tdQoQ6-X_Jf4-WEZoDiwt5oaS5aVDDL81tRQpizN0ZNQpOmVmhaOUWajG-72do3Lg20ZsxHyCN-3Qa7UNds1SwRIjN1PG5CT2dpA2awk6wiXodueRqXyvwYcnWDkCFYkVwfR4J8gy7MSMmSfcYCnw1gEhAUWDI4qV_isboSPTOVf1PZGhTm8xfHqYqo4pMtQ3Gv4z0oRLTxRw",
         "scope" : "GOOGLE",
         "types" : [ "store", "establishment" ],
         "vicinity" : "1703 W Main St, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.414808,
               "lng" : -89.069509
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
         "id" : "3ead86676b6f6568c0e24da6c891ac1d593d7e86",
         "name" : "Magnum Inn",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 515,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAA7SYZCRH1mXcEbla20tlmqDvjJ-mED8jcU4FlvdS9BzGe4wQUbmO5y-jZxMhD-l0bEM_pL73A-LlnOeX70yhSGBtAgh-w00AM6g13RRKTq81PcY3CvmYosVtQNsh2mLqYbjeD3y8fZGVOyfxgjzsZ0BIQDI3wLv_OzbkNd6JcaIKYAhoUIZ2Ix4J9zJx3cIx0T4pF1t0nlOE",
               "width" : 664
            }
         ],
         "place_id" : "ChIJHdtRaGxteYgRWrmAQrB1Ii0",
         "rating" : 3.2,
         "reference" : "CnRsAAAAuz5DdytgTSpPYe9SOV8pdEbhC0QdZaJFgqPNcX2Dh2Ptv3Dizg3hh53J-ggS9VnreSxEompsNSlEDMqyMxcSmyWBGeXX-lp3ouGlwd1jyQ2xrSk555DSKUFsTXOb09VYlkz49bHPcRrFgBTrCNxgHBIQ1q74HYeeh4HPa4RuEnOkJxoUxq1xb6-xP5fUpGhASLNoOcV5HSc",
         "scope" : "GOOGLE",
         "types" : [ "lodging", "establishment" ],
         "vicinity" : "1400 Vaden St, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.414686,
               "lng" : -89.071044
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "e206dc53da4ea1b97a55b3b6ddc07c4213124778",
         "name" : "McDonald's",
         "photos" : [
            {
               "height" : 1600,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAAkcvGJsmPH82omSrbCMvtS-W4GlqSQVW2RVmVwMN5QCuy-ysMZTMhjNJKniCq1Z1X_ft_WifkrdHC4CZMjk9Cc7oLJJ86eH8ym0l4GXWL2gaB0WDlTPPAwTJvNLLo9XbQzO-iGedztwb09lGu9C-CRBIQOF-nV0oznb2rTXUbeBM1cxoUwzDUGA-Gp4diin3Fgys2OLCzj_U",
               "width" : 2400
            }
         ],
         "place_id" : "ChIJaSpe2CINeYgRRdqBU2oWmD4",
         "price_level" : 1,
         "rating" : 3.8,
         "reference" : "CnRsAAAARdHgTE--6QS58qBqc4-Lwn2yQ7ZbVd38QxiaTNlNfpEqPdx7jVHVs8Dyv1Wb3_24dnYO0JMSoo_WCLIbCvfJoz-cLPbWUQmJOl8ed00FInYB2GvsAz47NXs2hbdDWO-ddWovM0Gy4U9UQz_ESX0WZBIQYE0wfyW6asGyXtH-vn-w7hoUpdg4kroNYG1RLmAONay-9drr1V0",
         "scope" : "GOOGLE",
         "types" : [ "restaurant", "food", "establishment" ],
         "vicinity" : "1108 Marshall Rd, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.41616,
               "lng" : -89.071546
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
         "id" : "dde300ea80effd66bc5207653522aa6888871b1b",
         "name" : "Hospitality House",
         "photos" : [
            {
               "height" : 1265,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRoAAAA-MtwjZNgEbMP-spDyTJMJ7pvScfDFlNHsjHf38uUdgdn8CBa0xALZV5jWNmc1gdrTSMNazjLj1jp8cHtQu0CL6OeTUq1hVep7RGCTNZfTPwU1MoG0yClTbJ_brB_ieYhF7ORMx55Zdcdl5zqd0w_aRIQZPIybu7WZEFcs---WoULaRoU4Ecefafk7TpDNGQ-aIA9_gv64CI",
               "width" : 949
            }
         ],
         "place_id" : "ChIJe4WHxmtteYgRNPzWmWexm9E",
         "reference" : "CoQBdAAAAEjmYAFuqnhKyk5KH-0RgtbCYD5IspsLsIlwSVvbD5UHp2XHattMzO8SXxoqhUCQRwjITNGgivh4uUz4PowbHfBm-23Oo4UAhjo0ddyq7H-wyxNqEnG5YLNG4Uh-TJ6j9fpneNkeAZ1GcznRaP2GaFn28ambhVwFjgmjmnnP4bMkEhDdZqlmDbnlzko4lNUUrDyDGhTiucH0xvWht6bKNdZ3Gi9i0WZvAQ",
         "scope" : "GOOGLE",
         "types" : [ "lodging", "establishment" ],
         "vicinity" : "1221 W Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.418675,
               "lng" : -89.04506499999999
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "3628f4542d80aeb32660cc79f199f5bb093f8f06",
         "name" : "Union City High School",
         "place_id" : "ChIJN0Vo8ZBteYgRuFAUlym_RTQ",
         "reference" : "CoQBeAAAAJ1MoO6KBsch6JL6f9774kdThqQjhjZwtjvtt0SRNhEh9LFBra8isVJNdauAmJQdIBn6HoesHrXncr2MeVa3SYolhFAwwa370SuXblaxXOUCv2QPOx3g-B4yuBIqbrkWCqjyd7kx-yQiPW-O65MWyxJRb_DO6_LtCFSu9JG69NuvEhAxL3jfVvBmPM4Jerdx8gTrGhT_Yr52JyppxW-NdKbu03JpBRC5ZQ",
         "scope" : "GOOGLE",
         "types" : [ "school", "establishment" ],
         "vicinity" : "Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.412511,
               "lng" : -89.045258
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/library-71.png",
         "id" : "141eb9e97c250d38edea0e3fd79591c8624f0d07",
         "name" : "Obion County Public Library",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 320,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAA9hsRVRE1e98_-F1CZ_PyAMFJpp5XiFNVyArAVKpGKQCqNJPuzFh_K8AFYq28IwTlX7E5CaPIp-5GVspJFBo86NOeG51qwal4c2jtzG6yNn7e1KIZyZaAFJN0MedT0r10dyY-rLK3cTiRwVWMFAQoRhIQuJ4AmRN_nVqRTe3WmqxUcBoU1RbUbj6vvX2_Czm7_vUWS9fICFM",
               "width" : 714
            }
         ],
         "place_id" : "ChIJJeu2nZdteYgRjpYvbdjjo5o",
         "reference" : "CoQBfgAAAKVkJ-jzc8uvL3j1BRWRsCf8XYmYIrVwAGQ6e0_I0R4ESGwPONmr2YjzpJM7M-kBkDJzadqM2akNIZGnx8EZO6yNQwJixaM7M_iRJf5Z8hPQ5DrET-3Okz-ekm8N7Hddl0whhdpIgJ3BSO40IxNdY9AneIrnEILSNN_Ehw2VHz5SEhBIOoHmKZlW-eY_5DAT-dsRGhRb6M7AaIAazZp6tfho8b0JRVcT0Q",
         "scope" : "GOOGLE",
         "types" : [ "library", "establishment" ],
         "vicinity" : "1221 E Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.406457,
               "lng" : -89.083229
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/car_dealer-71.png",
         "id" : "534f7d428e684b505c0201fbd5bec92c7350140a",
         "name" : "Herman Jenkins Motors",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 300,
               "html_attributions" : [],
               "photo_reference" : "CoQBegAAAHgkY0O1GkQOw3UyK01k5kRT5wHsu-LrA0foYbe6EYp_8kxMKWvZ0avSh9-q8FcwuPLzatVIKHgqyhbtsw0vCQTwm14rVpj5AuChCmMA6ZDkW-HLDYt3ChIu2ZOAXlJ81iV-qT3-JF6fhnKg5kvnLi9oLJLQB8tAocfn_tKDG1PpEhCVi0ZlCV4YukRqCDJiquxFGhS-D5IqVXWZUOPCDDhvJnL7gWJsew",
               "width" : 400
            }
         ],
         "place_id" : "ChIJvc3a7TlteYgRNOz3Bkin4XA",
         "reference" : "CoQBdwAAAOkmhodiriORekFL_b4sxn4mIZ49xqBNUZlTiAIdgVPaw4aol2MLqp07tVIeZioUKUFAE-dfbkDmITJALg4isxIosqXOtTLhB4uPeaRrLgZbbpMbHYhgTmEEJ8iaFeLZDp6hL9rdsMl2NOEq1skaJBBfWFhG7PyBF-H4C3B3uj-VEhCyzXulS4egMMIuoFZlyRnvGhQx46qeBl0vVln_ikoFG72K9QyP9A",
         "scope" : "GOOGLE",
         "types" : [ "car_dealer", "store", "establishment" ],
         "vicinity" : "2030 W Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.414527,
               "lng" : -89.051807
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "9aef40412768234fb77a9183312c9c2a30142fb6",
         "name" : "Alexander Thompson Arnold CPAs",
         "opening_hours" : {
            "open_now" : false
         },
         "place_id" : "ChIJ5zdFxJ5teYgRXJniK5gkCHY",
         "reference" : "CoQBgAAAAO-XvnNMnKnJbvYZssbxRV_HD7S2ofQ1tZaS3cOZwOnzc6qBD06ZSpoflowcIQ2rzg2ZK4F8pfQYm2kXlhMjaxfMjp2C4Wn9ykgFVaFFMX2QYp89HfKBxw5L1UKBST-_E7gwkMfiatpqMhtp6Dqa3_uvV3Rh-YUvw-J48yTA-uYEEhBg-JMFROb3XLiB-cJKfhceGhQlswlRmujzIAH1CfZ1m4jTAi6INg",
         "scope" : "GOOGLE",
         "types" : [ "accounting", "finance", "establishment" ],
         "vicinity" : "624 E Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.41167,
               "lng" : -89.07716000000001
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "4febc74c2caa824003436f9d157fc77241acef16",
         "name" : "Allen's Towing and Recovery",
         "opening_hours" : {
            "open_now" : false
         },
         "photos" : [
            {
               "height" : 496,
               "html_attributions" : [ "From a Google User" ],
               "photo_reference" : "CnRoAAAAaIBnBsrGyI63Y1i_1rqk7ZjT35teVKxMlelafkeJ3xPEQGatv3dUfmSWdx4a9MU0ipaBdOIktcx9pSOfUjMGJ1uwl7kN3_q5WWO7WSeT2dm80zqixHbJvxHi18xL8MAG4eQMjiYfm1sCt6ktk8XE3BIQ6QLxP3-Ti8Q7a6yD79thbBoUUcPS6dLKTN2BZFs3d0VSTOQUZ5c",
               "width" : 664
            }
         ],
         "place_id" : "ChIJzVNIekBteYgRpTSyhoSmYNY",
         "reference" : "CoQBfgAAAI2pqvJEszvL2ALUc3VJJND4f0huVfNOiFQDF8Yss2BCkAS1R0RMOekhoPCIM1GehW1rvJrOW9L1fwOm2uFPOjBz6vwfdf2J9cxdI9j7gbwffLjhkIroybpuJymU9wG36sE78SnusgC9R4EmsRmAQboubgmPlkba88ClUKMo4FPGEhA4eN8W949GvxWDpPjDR2AJGhSSw-3rMVcrvWGJSLGxRyJh0iwyvg",
         "scope" : "GOOGLE",
         "types" : [ "store", "car_repair", "establishment" ],
         "vicinity" : "2013 W Reelfoot Ave, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.415154,
               "lng" : -89.059021
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
         "id" : "44972998726f9c65ac6ef1ffaf5b3cea2054b410",
         "name" : "Hill's Hardware & Sporting",
         "opening_hours" : {
            "open_now" : false
         },
         "photos" : [
            {
               "height" : 344,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAAvOS7rQ6ysF4pXNgkp1ekSDl51NoZiIFnUSESNON7iokoHgwcfm1_eBmmMiCtuDiWh6jqpczuWzKJXUkU8agTrsPEHEb8ns3wQ1oM25kB28Wwa2Q9UtWE_seCCN4rKDN6K_fkwsx8YvvXn4fpQOmrxhIQgZrDkuqs8g43q-ZrvsOtKBoUq48-QcRr3X8IHUGegXRouF3y43w",
               "width" : 695
            }
         ],
         "place_id" : "ChIJUT0oSHRteYgR2Qh_T5F-jHQ",
         "price_level" : 3,
         "reference" : "CoQBfAAAACmYowpVMcpvmBr145AolKzavU4SdyUcky8THSq2i3Ip1QymtiaXYTdc3g6N7oFcRjKqoeud5_Fm3rNIf3u06k9oYEg50tFq9eqxQx6RjFFSdTpKKsMmtUIMItEKttEUlEJ0547UONEA2eZoIB6tU_SpR1NdAEJn3hgko_HIAr1ZEhCh8D4vX5LlLZCqbXyzNJ6cGhQs7tht72l7vI2gB76fSUBcQgJGrA",
         "scope" : "GOOGLE",
         "types" : [ "home_goods_store", "hardware_store", "store", "establishment" ],
         "vicinity" : "1234 S 2nd St, Union City"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 36.422949,
               "lng" : -89.057303
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "d3e7293fb79270f125bce0a9c1486cc4e49dad44",
         "name" : "Main Street Union City Inc",
         "place_id" : "ChIJ-x4yPnhteYgRx1FWBXkWxus",
         "reference" : "CoQBfQAAAHLx1vp7aheY1hXZrYCqioCL9AGuIOhbjLjekog1lYvOpNJQDdVrX-MlroOVnyHEEYz71mDVif2eER-daP4AFou3B8746fGcVcO-LdcmzYK9AbSpsh-619REM0wU6_QDm14SIRFG25PQ3Wshb_GFioyYDcOmlDVdHZDKu8AZGXz2EhAZKRQaoj3svdHmFuf5S0ilGhT2jvPtbczu7ldaWksTAfWkoykdFQ",
         "scope" : "GOOGLE",
         "types" : [ "establishment" ],
         "vicinity" : "210 S 1st St, Union City"
      }
   ],
   "status" : "OK"
}
EOD;

// WHAT HAVE WE GOT?
$obj = json_decode($jso);
var_dump($obj);

// IF DECODING FAILS?
if (!$obj) echo json_last_error_message();

function json_last_error_message()
{
    static $errors = array
    ( JSON_ERROR_NONE           => null
    , JSON_ERROR_DEPTH          => 'Maximum stack depth exceeded'
    , JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch'
    , JSON_ERROR_CTRL_CHAR      => 'Unexpected control character found'
    , JSON_ERROR_SYNTAX         => 'Syntax error, malformed JSON'
    , JSON_ERROR_UTF8           => 'Malformed UTF-8 characters, possibly incorrectly encoded'
    )
    ;
    // ADD THESE AT PHP 5.5+ OR USE json_last_error_msg()
    if (defined('JSON_ERROR_RECURSION'))        $errors[JSON_ERROR_RECURSION]        = 'One or more recursive references in the value to be encoded';
    if (defined('JSON_ERROR_INF_OR_NAN'))       $errors[JSON_ERROR_INF_OR_NAN]       = 'One or more NAN or INF values in the value to be encoded';
    if (defined('JSON_ERROR_UNSUPPORTED_TYPE')) $errors[JSON_ERROR_UNSUPPORTED_TYPE] = 'A value of a type that cannot be encoded was given';

    $error = json_last_error();
    return array_key_exists($error, $errors)
    ? $errors[$error]
    : "Unknown error ({$error})"
    ;
}

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40404252
Regarding this statement:

$json_data = json_decode($poiAddress);

How do you know whether json_decode() worked or not?
http://php.net/manual/en/function.json-last-error.php
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now