I'm playing with Google Maps and Geocoding (converting Address to lang/lat coordinates). Everything works fine so far and I'm getting an answer from the google maps server with a file:
$address = urlencode( $address );
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}&sensor=false";
$content = file_get_contents( $url );
echo '<pre>';
print_r( $content );
echo '</pre>';
Point is that I don't need all that information.
Questions
A How do I get only a part of the JSON object?
(Example: I only need 'results'->'address_components'->'type' = 'country';.)
B Can I reduce the content of the returned (google) file somehow as I don't need everything?
Update
I need to add part of the data to a json object. So decoding & then encoding seems like a loss of energy & time (at this point). Any ideas if I'd be better (faster) off with requesting a XML object? (The final set of objects is pretty large. This is why I should really care about performance.)
Example JSON answer from Google Maps maps server (file content):
{
"results" : [
{
"address_components" : [
{
"long_name" : "28",
"short_name" : "28",
"types" : [ "street_number" ]
},
{
"long_name" : "Sellhorner Weg",
"short_name" : "Sellhorner Weg",
"types" : [ "route" ]
},
{
"long_name" : "Behringen",
"short_name" : "Behringen",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Bispingen",
"short_name" : "Bispingen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Soltau-Fallingbostel",
"short_name" : "Soltau-Fallingbostel",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Niedersachsen",
"short_name" : "NDS",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "29646",
"short_name" : "29646",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Sellhorner Weg 28, 29646 Bispingen, Deutschland",
"geometry" : {
"location" : {
"lat" : 53.118080,
"lng" : 9.966859999999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 53.11942898029150,
"lng" : 9.96820898029150
},
"southwest" : {
"lat" : 53.11673101970850,
"lng" : 9.965511019708496
}
}
},
"partial_match" : true,
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
$array = json_decode($content, true);
$countries = array_filter($array['results']['address_components'], function ($a) {
return in_array('country', $a['types']);
});
var_dump($countries);
Gives you all address_components which have a type 'country'. Probably that's only going to be one, but it may be more than one. This syntax requires PHP 5.3+ BTW.
Can I reduce the content of the returned (google) file somehow as I don't need everything?
Just take the parts of the decoded $array you need and forget the rest. If you want, json_encode it again.
Related
So I am retrieving a json file from an API, and I want to grab the data from object 'place_id'.
I have been looking online and I have seen how to grab elements from json files, however I can't understand what I am doing wrong, as my code gives me an error:
Warning: Undefined array key "place_id" in C:\xampp\htdocs\cw\tester.php on line 15
This is my php code where I try to grab the data from 'place_id':
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results']['place_id'];
this is my json file:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Antrim Road",
"short_name" : "Antrim Rd",
"types" : [ "route" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Newtownabbey",
"short_name" : "Newtownabbey",
"types" : [ "postal_town" ]
},
{
"long_name" : "Belfast",
"short_name" : "Belfast",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Northern Ireland",
"short_name" : "Northern Ireland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "BT36 7PN",
"short_name" : "BT36 7PN",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Antrim Rd, Belfast, Newtownabbey BT36 7PN, UK",
"geometry" : {
"location" : {
"lat" : 54.6577821,
"lng" : -5.943665699999999
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 54.65913108029149,
"lng" : -5.942316719708497
},
"southwest" : {
"lat" : 54.6564331197085,
"lng" : -5.945014680291502
}
}
},
"place_id" : "ChIJ88iCe5inYUgRdeXmfCtuvu4",
"plus_code" : {
"compound_code" : "M354+4G Belfast, UK",
"global_code" : "9C6PM354+4G"
},
"types" : [ "establishment", "point_of_interest", "tourist_attraction", "zoo" ]
}
],
"status" : "OK"
}
Apologies as I know it is obvious, but I can't seem to understand why! thanks!
This should do the trick? Notice the [0] that was added. Since results is actually an array.
<?php
$place_json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=belfast+zoo&key=' . $place_id_key);
$json = json_decode($place_json, true);
echo $json['results'][0]['place_id'];
I'm using Google Map Api, and getting the result in JSON form and the output result is
{
"results" : [
{
"address_components" : [
{
"long_name" : "Plot No 1",
"short_name" : "Plot No 1",
"types" : [ "premise" ]
},
{
"long_name" : "B Block Road",
"short_name" : "B Block Rd",
"types" : [ "route" ]
},
{
"long_name" : "Rangpuri Extention B-Block Pocket-4",
"short_name" : "Rangpuri Extention B-Block Pocket-4",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South West Delhi",
"short_name" : "South West Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110037",
"short_name" : "110037",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Plot No 1, B Block Rd, Rangpuri Extention B-Block Pocket-4, New Delhi, Delhi 110037, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.5379687,
"lng" : 77.121816
},
"southwest" : {
"lat" : 28.5377897,
"lng" : 77.1217006
}
},
"location" : {
"lat" : 28.5378435,
"lng" : 77.1217523
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 28.5392281802915,
"lng" : 77.12310728029151
},
"southwest" : {
"lat" : 28.5365302197085,
"lng" : 77.12040931970849
}
}
},
"partial_match" : true,
"place_id" : "ChIJibjV7RYcDTkRnaFq2Lg__b4",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
and I want to extract lat and lng,under the location object, write now I'm using this code
$mapcontent = json_decode($content,true);
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
where $content is the output result which mention above,by using the above code and it works but sometimes I'm getting error of undefined offest:0, on $lat and $lng line, is there any other way to retrieve specific data from the JSON Code, I already search on internet but I'm not getting any idea how to resolve this, please help me out
You can write the code like :
$mapcontent = json_decode($content,true);
$resultCount = count($mapcontent['results']);
if( $resultCount > 0 ) {
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}
It will work.
$url='your url';
$array = file_get_contents($url);
$array = (array)json_decode($array);
You can also write code like.
$mapcontent = json_decode($content,true);
if( true != empty($mapcontent['results'])) {
$lat = $mapcontent['results'][0]['geometry']['location']['lat'];
$lng = $mapcontent['results'][0]['geometry']['location']['lng'];
}
I am looking for a way to find the PlaceID from a Google web request. I have several hundred addresses (i.e. coordinates) and I want to find a way to all enter them in a phpscript.
I am struggling with the API request for the place API. I have the following request:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=50.096332,8.673130&radius=1&key=APIKEY
This returns the entire city of Frankfurt, but that's not what I am after. If I enter the address Oskar-Sommer-Strasse 7, Frankfurt am Main, Deutschland into the PlaceID finder on the Google Maps documentation site, I get the following result for the ID ChIJ-b9GgAUMvUcRYuYKJtGGlw4.
I am now looking for a way to get this done. I've collected the coordinates using Google Maps API as well, so they should match (or at least that's what I thought).
Maybe I need to use some other request type?
Using the geocode api and sending a request to
https://maps.googleapis.com/maps/api/geocode/json?address=Oskar-Sommer-Strasse+7+%20Frankfurt%20am%20Main%20&component=Deutschland
returns (I'm assuming you want the place id field)
{
"results" : [
{
"address_components" : [
{
"long_name" : "7",
"short_name" : "7",
"types" : [ "street_number" ]
},
{
"long_name" : "Oskar-Sommer-Straße",
"short_name" : "Oskar-Sommer-Straße",
"types" : [ "route" ]
},
{
"long_name" : "Frankfurt am Main Süd",
"short_name" : "Frankfurt am Main Süd",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Frankfurt am Main",
"short_name" : "Frankfurt am Main",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Darmstadt",
"short_name" : "DA",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Hessen",
"short_name" : "HE",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Germany",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "60596",
"short_name" : "60596",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Oskar-Sommer-Straße 7, 60596 Frankfurt am Main, Germany",
"geometry" : {
"location" : {
"lat" : 50.09665,
"lng" : 8.67299
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 50.0979989802915,
"lng" : 8.674338980291502
},
"southwest" : {
"lat" : 50.0953010197085,
"lng" : 8.671641019708497
}
}
},
"place_id" : "ChIJ-b9GgAUMvUcRYuYKJtGGlw4",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
I'm currently using Google Maps API to get a longitude and latitude form the user's street, street number and zip code.
I would also like to grab their city and state from the Maps API results. The XML document formats it strangely though, and I'm not sure how I'm supposed to reference it (it's different from the way I'm grabbing the long and lat).
This is how I am loading the Maps API:
$map_address = str_replace($address."+".$zip);
$map_api_url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$map_address."&key=myKey";
$resp_json = file_get_contents($map_api_url);
$resp = json_decode($resp_json, true);
I then get the Long and Lat:
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
But how do I grab the city and state?
{
"results" : [
{
"address_components" : [
{
"long_name" : "3840",
"short_name" : "3840",
"types" : [ "street_number" ]
},
{
"long_name" : "Fake Street",
"short_name" : "Fake St",
"types" : [ "route" ]
},
{
"long_name" : "Wilson Heights",
"short_name" : "Wilson Heights",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "North York",
"short_name" : "North York",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Toronto",
"short_name" : "Toronto",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Toronto Division",
"short_name" : "Toronto Division",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Ontario",
"short_name" : "ON",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Canada",
"short_name" : "CA",
"types" : [ "country", "political" ]
},
{
"long_name" : "M6H",
"short_name" : "M6H",
"types" : [ "postal_code_prefix", "postal_code" ]
}
],
"formatted_address" : "3840 Fake Street, Toronto, ON M6H, Canada",
"geometry" : {
"location" : {
"lat" : 43.5399244,
"lng" : -78.43486559999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 43.5412733802915,
"lng" : -78.43351661970848
},
"southwest" : {
"lat" : 43.7385754197085,
"lng" : -79.43621458029151
}
}
},
"place_id" : "ChIJPVDxjGgyK4gRt3O7zOSmIF4",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Just answered my own question by running the code through a loop. Please let me know if you have a better way!
foreach($resp['results'][0]['address_components'] as $address_componenets) {
if($address_componenets["types"][0]=="country") {
$country = $address_componenets["long_name"];
}
if($address_componenets["types"][0]=="administrative_area_level_1") {
$state = $address_componenets["long_name"];
}
if($address_componenets["types"][0]=="locality") {
$city = $address_componenets["long_name"];
}
}
I want to fetch Postcode [ "long_name" : "S35 1TZ",] from this google api array
Iam usin this API
http://maps.googleapis.com/maps/api/geocode/json?address=(53.4624118,%20-1.4922259999999596)
Using
msg.results[0].address_components[6].long_name;
Not working
{
"results" : [
{
"address_components" : [
{
"long_name" : "7A",
"short_name" : "7A",
"types" : [ "street_number" ]
},
{
"long_name" : "Hollow Gate",
"short_name" : "Hollow Gate",
"types" : [ "route" ]
},
{
"long_name" : "Chapeltown",
"short_name" : "Chapeltown",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Sheffield",
"short_name" : "Sheffield",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Sheffield",
"short_name" : "Sheffield",
"types" : [ "postal_town" ]
},
{
"long_name" : "South Yorkshire",
"short_name" : "South York",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "S35 1TZ",
"short_name" : "S35 1TZ",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "7A Hollow Gate, Sheffield, South Yorkshire S35 1TZ, UK",
"geometry" : {
"location" : {
"lat" : 53.4624118,
"lng" : -1.492226
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 53.4637607802915,
"lng" : -1.490877019708498
},
"southwest" : {
"lat" : 53.4610628197085,
"lng" : -1.493574980291502
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
There are two ways. Based on your code, I am not sure if you got a string of that data or already an object. If you only got data back, it is a string and you first have to convert the JSON into PHP array or object.
1 - You could convert the JSON to an array:
$msg = json_decode($msg,true);
/* And call it by */
echo($msg['results'][0]['address_components'][7]['long_name']);
OR
2 - Using the object notation:
$msg = json_decode($msg);
/* And call it by */
echo($msg->results[0]->address_components[7]->long_name);
If you got the data back using a Google API PHP libary, it's probably not a string but an object so you can dismiss the json_decode() and use option 2. If you want to prefer using option 1, you can cast the object into an array by using $msg = (array)$msg;
In general using print_r($msg) or var_dump($msg) can be handy to debug the thing, just add your path each time you get closer to your goal. So in this case var_dump($msg) then var_dump($msg->result), then var_dump($msg->result[0]), and so on.