Hi All,

I have an API key for google maps and its giving me the following response.

My first question is, how can i extract the data? Normally i work with XML responses via CURL so im new to this.

If i do use the XML example i just get access denied

{
  "name": "1600 Amphitheatre Parkway, Mountain View, CA",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "AddressDetails": {
   "Accuracy" : 8,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "CA",
         "SubAdministrativeArea" : {
            "Locality" : {
               "LocalityName" : "Mountain View",
               "PostalCode" : {
                  "PostalCodeNumber" : "94043"
               },
               "Thoroughfare" : {
                  "ThoroughfareName" : "1600 Amphitheatre Pkwy"
               }
            },
            "SubAdministrativeAreaName" : "Santa Clara"
         }
      },
      "CountryName" : "USA",
      "CountryNameCode" : "US"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 37.4251466,
        "south": 37.4188514,
        "east": -122.0811574,
        "west": -122.0874526
      }
    },
    "Point": {
      "coordinates": [ -122.0850990, 37.4227820, 0 ]
    }
  } ]
}
function get_lat_long($postcode) {
        #change to your local domain i.e. .com
	$domain = "co.uk";

	$url = "http://maps.google." . $domain . "/maps/geo?q=" . urlencode($postcode) . "&output=json&key=".maps_key;

	$curl = new curl();
	$json = $curl->get_page(array("url"=>$url));

	$store_data = json_decode(str_replace(""","\"",htmlentities($json))); //Take care of accents

	$lng = $store_data->Placemark[0]->Point->coordinates[0];
	$lat = $store_data->Placemark[0]->Point->coordinates[1];

	//Return
	if($lng && $lat) {
		return array('lat'=>$lat,
		'lng'=>$lng
		);

	} else {

		return false;

	}

}

#this extracts the lat and long from the bottom - to call it use  this command:

$coords = get_lat_long('CA 94043');

#retults are returned like so

$coords['lat']; //latitude
$coords['lng']; // longtitude

Hi,

thanks for the response

I tried it out first time, the page gives a blank response, and ive echod out every line. im sure ill figure it out.

I noticed your using CURL to process the page. I usually use curl to process soap responses. What sort of response is this?

json (JavaScript Object Notation), have you put your key in where is says maps_key? sign up for both a maps key and an ajax key - they are identical for the site, just to be safe.

ok i will do that now.

I dont suppose you know how to get an xml key?

this is the code that returns white.

<?php
function get_lat_long($postcode) {
        #change to your local domain i.e. .com
echo	$domain = "co.uk";
echo "domain<br><br>";
echo	$maps_key = "the key";
echo "maps key<br><br>";
echo	$url = "http://maps.google." . $domain . "/maps/geo?q=" . urlencode($postcode) . "&output=json&key=". $maps_key;
echo "url<br><br>";
echo	$curl = new curl();
echo "curl<br><br>";
echo	$json = $curl->get_page(array("url"=>$url));
echo "json<br><br>";
echo	$store_data = json_decode(str_replace("&quot;","\"",htmlentities($json))); //Take care of accents
echo "store data<br><br>";
echo	$lng = $store_data->Placemark[0]->Point->coordinates[0];
echo "$lng<br><br>";
echo	$lat = $store_data->Placemark[0]->Point->coordinates[1];

	//Return
	if($lng && $lat) {
		return array('lat'=>$lat,
		'lng'=>$lng
		);

	} else {

		return false;

	}

}

#this extracts the lat and long from the bottom - to call it use  this command:
echo "lat<br><br>";
echo $coords = get_lat_long('NE1 1AA');
echo "postcode<br><br>";
#retults are returned like so

echo $coords['lat']; //latitude
echo $coords['lng']; // longtitude
?>

sorry for the confusion - the maps_key is supposed to be the api key, there is only the api key and the ajax key.

I didnt write this function but it works for me and UK postcode geocoding.

ok, thanks for your help. Im sure it will be my fault, ill have a play and see whats different and let you know.

Regards

Paul

hi,

ive had a play around with this and its not liking anything beyond line 8.
It just seems to stop.

Could you please check this against the code that you are using?

this is quite wierd.

the raw url gives a response, but it also gives xml if i change the output parameter to xml.

Now ive done a lot of coding with XML, CURL and SOAP, but CURL will simple not parse this response, not will your JSON.

Im continuing to investigate

Thats exactly what ive been doing. ive even changed the output type to xml and got a raw url response, but the output is blank.

Take the following code, using curl to process the request. The raw url gives a response but runnung the code gives nothing (i know ive not gone into parsing the xml)

<?php

//the raw url
//http://maps.google.co.uk/maps/geo?q=NE1 1AA&output=xml&key=ABQIAAAAVHrPYH_Sk6LVCesA5cvrhSBDl4JnyGZq3II_C_YS5ykoCjTcBRI61RIZRv6SGWAhpvhTZdb-6YsXQ
$url = 'http://maps.google.co.uk/maps/geo?';
$postdata 'q=NE1 1AA&output=xml&key=ABQIAAAAVHrPYH_Sk6LVCesA5cvrhSBDl4JnyGZq3II_C_YS5ykoCjTcBRI61RIZRv6SGWAhpvhTZdb-6YsXQ';
$result = request($url, $postdata);
?>

<?php 
function request($url, $postdata) {
$curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, $url);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($curl, CURLOPT_POST, 1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
   curl_setopt($curl, CURLOPT_HEADER, 0);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec($curl);
   curl_close($curl);
   return $result; 
}
?>

progress

{ "name": "0.000000,0.000000", "Status": { "code": 400, "request": "geocode" } }

#http://maps.google.co.uk/maps/geo?q=NE1%201AA&output=json&key=ABQIAAAAVHrPYH_Sk6LVCesA5cvrhSBDl4JnyGZq3II_C_YS5ykoCjTcBRI61RIZRv6SGWAhpvhTZdb-6YsXQ

{
  "name": "NE1 1AA",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "Newcastle Upon Tyne NE1 1, UK",
    "AddressDetails": {
   "Accuracy" : 5,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "Newcastle Upon Tyne",
         "SubAdministrativeArea" : {
            "Locality" : {
               "LocalityName" : "Newcastle Upon Tyne",
               "PostalCode" : {
                  "PostalCodeNumber" : "NE1 1"
               }
            },
            "SubAdministrativeAreaName" : "Newcastle Upon Tyne"
         }
      },
      "CountryName" : "UK",
      "CountryNameCode" : "GB"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 54.9734515,
        "south": 54.9626769,
        "east": -1.6062257,
        "west": -1.6221915
      }
    },
    "Point": {
      "coordinates": [ -1.6135470, 54.9694353, 0 ]
    }
  } ]
}

I really dont see how you can have a problem with the json output? confusing indeed. cURL can take a JSON response I do it nearly several thousand times a day. There is either something wrong with the request URL or we are missing something from the parser code.

i left out my cURL class sorry:

class curl {

	/**
	* COnstructor
	*/
	function curl() {



	}

	function init_curl($ch,$url,$postfields=null,$follow=null,$cookie=null,$referer=null) {

		// Set url
		curl_setopt($ch, CURLOPT_URL, $url);

		// Enable Post
		if($postfields) {
			curl_setopt ($ch, CURLOPT_POST, 1);
			curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
		}

		if($follow) {
			curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1 );
		}

		if($referer) {
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}

		//Enable SSL
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);


		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');

		//Return results as string
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

		return $ch;

	} // end function


	/*
	Grabs a page
	*/

	function get_page($options) {

		//Set options
		foreach($options AS $key=>$value) {
			$$key = $value;
		}

		$ch = curl_init();
		$ch = $this->init_curl($ch,$url);
		$page = curl_exec($ch);
		curl_close($ch);
		return $page;


	}


} // end class

That is the exact same response i got. Really strange. Im wondering if its something to do with a setting on your server perhaps?

json
json support enabled
json version 1.2.1

thats my phpinfo on it

aha, maybe thats it :-)

bingo! thanks for your help. very much appreciated

bingo! thanks for your help. very much appreciated

Sorry for the confusion - currently working makes a little hard to keep everything in mind. Rep me up please :D

oh, just thought of 1 more question.

i want to extract more info from the data so i followed the syntax of

$lat = $store_data->Placemark[0]->Point->coordinates[1];

as i want to get the 4 other markers "LatLongBox"
Tried a few combinations but no joy

I havent used it before but I assume its either similar to:

$lat = $store_data->Placemark[0]->ExtendedData->LatLonBox->north[0];

or 

$store_data->Placemark[0]->ExtendedData->LatLonBox->north.nodeValue;

Im afraid im a stumped but if you look up how json parses you might be able to figure our the syntax.

ok brilliant. Ill give it a whirl.

thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.