Jump to content

Google Places API


timmah1

Recommended Posts

For every search, I get ZERO RESULTS, and I don't understand what I'm doing wrong.

 

<?php
require('places_class.php');

$types = $_POST['type'];
$location = $_POST['place'];
$radius = $_POST['radius'];

$gplaces = New GooglePlaces;
$gplaces->SetLocation("$location");
$gplaces->SetRadius($radius);
$gplaces->SetTypes("$types");
$results = $gplaces->Search();

print_r($results);
?>

 

That's when the form is submitted, I checked the variables, and they are echoing out correctly.

 

Then I thought I'd hard-code the values in, same results

<?php
require('places_class.php');

$gplaces = New GooglePlaces;
$gplaces->SetLocation("40.5267,81.4778");
$gplaces->SetRadius(50);
$gplaces->SetTypes("food");
$results = $gplaces->Search();

print_r($results);	
?>

 

Here is the class

<?php
class GooglePlaces
{
	private $APIKey = "";

	public $OutputType = "json";  //either json, xml or array

	public $Errors	= array();

	private $APIUrl = "https://maps.googleapis.com/maps/api/place";
	private $APICallType = "";

	private $IncludeDetails = false; 

	//all calls
	private $Language		= 'en'; 	//optional - https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1

	//Search
	private $Location;			 		//REQUIRED  - This must be provided as a google.maps.LatLng object.
	private $Radius;			 		//REQUIRED  
	private $Types;				 		//optional - separate tyep with pipe symbol  http://code.google.com/apis/maps/documentation/places/supported_types.html  

	private $Name;				 		//optional

	//Search, Details, 
	private $Sensor			= 'false';	//REQUIRED - is $Location coming from a sensor? like GPS?

	//Details & Delete
	private $Reference;

	//Add
	private $Accuracy;

	public function Search()
   		{
			$this->APICallType = "search";

			return $this->APICall();
		}			

	public function Details()
   		{
			$this->APICallType = "details";

			return $this->APICall();
		}

	public function Checkin()
   		{
			$this->APICallType = "checkin-in";

			return $this->APICall();
		}

	public function Add()
   		{
			$this->APICallType = "add";

			return $this->APICall();
		}

	public function Delete()
   		{
			$this->APICallType = "delete";

			return $this->APICall();
		}


	public function SetLocation($Location)
		{
			$this->Location = $Location;
		}
	public function SetRadius($Radius)
		{
			$this->Radius = $Radius;
		}
	public function SetTypes($Types)
		{
			$this->Types = $Types;
		}
	public function SetLanguage($Language)
		{
			$this->Language = $Language;
		}
	public function SetName($Name)
		{
			$this->Name = $Name;
		}
	public function SetSensor($Sensor)
		{
			$this->Sensor = $Sensor;
		}
	public function SetReference($Reference)
		{
			$this->Reference = $Reference;
		}
	public function SetAccuracy($Accuracy)
		{
			$this->Accuracy = $Accuracy;
		}
	public function SetIncludeDetails($IncludeDetails)
		{
			$this->IncludeDetails = $IncludeDetails;
		}

	private function CheckForErrors()
		{
			if(empty($this->APICallType))
				{
					$this->Errors[] = "API Call Type is required but is missing.";
				}
			if(empty($this->APIKey))
				{
					$this->Errors[] = "API Key is is required but is missing.";
				}

			if(($this->OutputType!="json") && ($this->OutputType!="xml") && ($this->OutputType!="json"))
				{
					$this->Errors[] = "OutputType is required but is missing.";
				}



		}



	private function APICall()
		{
			$this->CheckForErrors();

			if($this->APICallType=="add" || $this->APICallType=="delete")
				{
					$URLToPostTo = $this->APIUrl."/".$this->APICallType."/".$this->OutputType."?key=".$this->APIKey."&sensor=".$this->Sensor;

					if($this->APICallType=="add")
						{
							$LocationArray = explode(",", $this->Location);
							$lat = trim($LocationArray[0]);
							$lng = trim($LocationArray[1]);

							$paramstopost[location][lat]	= $lat;
							$paramstopost[location][lng]	= $lng;
							$paramstopost[accuracy]			= $this->Accuracy;
							$paramstopost[name]				= $this->Name;
							$paramstopost[types]			= explode("|", $this->Types);
							$paramstopost[language]			= $this->Language;
						}
					if($this->APICallType=="delete")
						{
							$paramstopost[reference]		= $this->Reference;
						}

					$result = json_decode($this->CurlCall($URLToPostTo,json_encode($paramstopost)));
					$result->errors = $this->Errors;
					return $result;

				}

			if($this->APICallType=="search")
				{
					$URLparams = "location=".$this->Location."&radius=".$this->Radius."&types=".$this->Types."&language=".$this->Language."&name=".$this->Name."&sensor=".$this->Sensor;
				}
			if($this->APICallType=="details")
				{
					$URLparams = "reference=".$this->Reference."&language=".$this->Language."&sensor=".$this->Sensor;
				}
			if($this->APICallType=="check-in")
				{
					$URLparams = "reference=".$this->Reference."&language=".$this->Language."&sensor=".$this->Sensor;
				}


			$URLToCall = $this->APIUrl."/".$this->APICallType."/".$this->OutputType."?key=".$this->APIKey."&".$URLparams;

			$result = json_decode(file_get_contents($URLToCall),true);

			$result[errors] = $this->Errors;

			if($result[status]=="OK" && $this->APICallType=="details")
				{
					foreach($result[result][address_components] as $key=>$component)
						{
							if($component[types][0]=="street_number")
								{
									$address_street_number = $component[short_name];
								}
							if($component[types][0]=="route")
								{
									$address_street_name = $component[short_name];
								}
							if($component[types][0]=="locality")
								{
									$address_city = $component[short_name];
								}
							if($component[types][0]=="administrative_area_level_1")
								{
									$address_state = $component[short_name];
								}
							if($component[types][0]=="postal_code")
								{
									$address_postal_code = $component[short_name];
								}
						}

					$result[result][address_fixed][street_number]			= $address_street_number;
					$result[result][address_fixed][address_street_name]		= $address_street_name;
					$result[result][address_fixed][address_city]			= $address_city;
					$result[result][address_fixed][address_state]			= $address_state;
					$result[result][address_fixed][address_postal_code]		= $address_postal_code;
				}
			return $result;

		}

	private function CurlCall($url,$topost)
		{
			$ch = curl_init(); 
			curl_setopt($ch, CURLOPT_URL, $url); 
			curl_setopt($ch, CURLOPT_HEADER, FALSE);  
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
			curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
			$body = curl_exec($ch); 
			curl_close($ch); 

			return $body;

		}

}
?>

 

And yes, I do have the correct API Key, I just took it out

 

Can anybody see what the issue is?

Link to comment
Share on other sites

I got this fixed, now, my next issue how can I get the array

Array
(
    [html_attributions] => Array
        (
        )

    [results] => Array
        (
            [0] => Array
                (
                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 40.522125
                                    [lng] => -81.477138
                                )

                        )

                    [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png
                    [id] => 6d56e92588d82f2b97eb3c8d106a89bec2189953
                    [name] => Bread Head Bakery & Coffee Shop
                    [rating] => 4.2
                    [reference] => CoQBfQAAANOKqBjJe3JWBRdY5Uc7NmdP-L6O3h_HDnJn3Mm6HGcnWADCJ3BjGB3YAq02fE9FAizTf_H43-GhX1lhdUp2iehFAvV4C-cH_QeZnVlOi3f0SEb48eOYoIWZ6QE-7lVtTW3IXEka7nsO6gMELb2BsFd7sNFT58OdRnaUjap59mQmEhBQ_M4Ztd86Nvr5A8o0z5W2GhTUM2OtHhQjhp301WiD-3l7N5GnLA
                    [types] => Array
                        (
                            [0] => bakery
                            [1] => cafe
                            [2] => restaurant
                            [3] => art_gallery
                            [4] => store
                            [5] => food
                            [6] => establishment
                        )

                    [vicinity] => 113 West 3rd Street, Dover
                )

            [1] => Array
                (
                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 40.52232
                                    [lng] => -81.482009
                                )

                        )

                    [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png
                    [id] => 95add20df007d2ae8fbdeaabcf02d3b3e7a0f023
                    [name] => Dairy Queen
                    [rating] => 3.3
                    [reference] => CnRqAAAAljDKB4A11DGFSte1LDYfFp3fly5fi9Bp2PgbmNEcwklA3tyHVik8qnB9GHvi08CymHLFOQL19B6HdEQXWB1clChyx6lSdHMk2nSk6nk5tXzwfAjIpfA4MUdoUzjzc9PjpIBm0ngMJUCcYYeJHMXkqhIQbWoTNKYhljBbWFNvlRq0RhoUK33sIdCZIRHdWqzveIfuYPYNyEo
                    [types] => Array
                        (
                            [0] => store
                            [1] => restaurant
                            [2] => food
                            [3] => establishment
                        )

                    [vicinity] => 501 North Tuscarawas Avenue, Dover
                )

to show me just the [lat] and [long] of the results?

 

Thanks

Link to comment
Share on other sites

I got this fixed, now, my next issue how can I get the array

Array
(
    [html_attributions] => Array
        (
        )

    [results] => Array
        (
            [0] => Array
                (
                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 40.522125
                                    [lng] => -81.477138
                                )

                        )

                    [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png
                    [id] => 6d56e92588d82f2b97eb3c8d106a89bec2189953
                    [name] => Bread Head Bakery & Coffee Shop
                    [rating] => 4.2
                    [reference] => CoQBfQAAANOKqBjJe3JWBRdY5Uc7NmdP-L6O3h_HDnJn3Mm6HGcnWADCJ3BjGB3YAq02fE9FAizTf_H43-GhX1lhdUp2iehFAvV4C-cH_QeZnVlOi3f0SEb48eOYoIWZ6QE-7lVtTW3IXEka7nsO6gMELb2BsFd7sNFT58OdRnaUjap59mQmEhBQ_M4Ztd86Nvr5A8o0z5W2GhTUM2OtHhQjhp301WiD-3l7N5GnLA
                    [types] => Array
                        (
                            [0] => bakery
                            [1] => cafe
                            [2] => restaurant
                            [3] => art_gallery
                            [4] => store
                            [5] => food
                            [6] => establishment
                        )

                    [vicinity] => 113 West 3rd Street, Dover
                )

            [1] => Array
                (
                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 40.52232
                                    [lng] => -81.482009
                                )

                        )

                    [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png
                    [id] => 95add20df007d2ae8fbdeaabcf02d3b3e7a0f023
                    [name] => Dairy Queen
                    [rating] => 3.3
                    [reference] => CnRqAAAAljDKB4A11DGFSte1LDYfFp3fly5fi9Bp2PgbmNEcwklA3tyHVik8qnB9GHvi08CymHLFOQL19B6HdEQXWB1clChyx6lSdHMk2nSk6nk5tXzwfAjIpfA4MUdoUzjzc9PjpIBm0ngMJUCcYYeJHMXkqhIQbWoTNKYhljBbWFNvlRq0RhoUK33sIdCZIRHdWqzveIfuYPYNyEo
                    [types] => Array
                        (
                            [0] => store
                            [1] => restaurant
                            [2] => food
                            [3] => establishment
                        )

                    [vicinity] => 501 North Tuscarawas Avenue, Dover
                )

to show me just the [lat] and [long] of the results?

 

Thanks

<?php
    $i=1;
    foreach( $results['results'] as $result ){
        print 'Result '.$i.': '.$result['geometry']['location']['lat'].','.$result['geometry']['location']['lng']."\n";
        $i++;
    }
?>

Link to comment
Share on other sites

I got this fixed, now, my next issue how can I get the array

Array
(
    [html_attributions] => Array
        (
        )

    [results] => Array
        (
            [0] => Array
                (
                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 40.522125
                                    [lng] => -81.477138
                                )

                        )

                    [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png
                    [id] => 6d56e92588d82f2b97eb3c8d106a89bec2189953
                    [name] => Bread Head Bakery & Coffee Shop
                    [rating] => 4.2
                    [reference] => CoQBfQAAANOKqBjJe3JWBRdY5Uc7NmdP-L6O3h_HDnJn3Mm6HGcnWADCJ3BjGB3YAq02fE9FAizTf_H43-GhX1lhdUp2iehFAvV4C-cH_QeZnVlOi3f0SEb48eOYoIWZ6QE-7lVtTW3IXEka7nsO6gMELb2BsFd7sNFT58OdRnaUjap59mQmEhBQ_M4Ztd86Nvr5A8o0z5W2GhTUM2OtHhQjhp301WiD-3l7N5GnLA
                    [types] => Array
                        (
                            [0] => bakery
                            [1] => cafe
                            [2] => restaurant
                            [3] => art_gallery
                            [4] => store
                            [5] => food
                            [6] => establishment
                        )

                    [vicinity] => 113 West 3rd Street, Dover
                )

            [1] => Array
                (
                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 40.52232
                                    [lng] => -81.482009
                                )

                        )

                    [icon] => http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png
                    [id] => 95add20df007d2ae8fbdeaabcf02d3b3e7a0f023
                    [name] => Dairy Queen
                    [rating] => 3.3
                    [reference] => CnRqAAAAljDKB4A11DGFSte1LDYfFp3fly5fi9Bp2PgbmNEcwklA3tyHVik8qnB9GHvi08CymHLFOQL19B6HdEQXWB1clChyx6lSdHMk2nSk6nk5tXzwfAjIpfA4MUdoUzjzc9PjpIBm0ngMJUCcYYeJHMXkqhIQbWoTNKYhljBbWFNvlRq0RhoUK33sIdCZIRHdWqzveIfuYPYNyEo
                    [types] => Array
                        (
                            [0] => store
                            [1] => restaurant
                            [2] => food
                            [3] => establishment
                        )

                    [vicinity] => 501 North Tuscarawas Avenue, Dover
                )

to show me just the [lat] and [long] of the results?

 

Thanks

<?php
    $i=1;
    foreach( $results['results'] as $result ){
        print 'Result '.$i.': '.$result['geometry']['location']['lat'].','.$result['geometry']['location']['lng']."\n";
        $i++;
    }
?>

 

Thank you. Exactly what I needed

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.