I have a json file like this:
{"results" : [
{
"address_components" : [
{
"long_name" : "4400",
"short_name" : "4400",
"types" : [ "postal_code" ]
},
{
"long_name" : "Upper Austria",
"short_name" : "OÖ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "4400, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.06669549999999,
"lng" : 14.4687485
},
"southwest" : {
"lat" : 47.9987654,
"lng" : 14.3408425
}
},
"location" : {
"lat" : 48.0418838,
"lng" : 14.4078882
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.06669549999999,
"lng" : 14.4687485
},
"southwest" : {
"lat" : 47.9987654,
"lng" : 14.3408425
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
and everytime i try to access it i get undefined index location, i want to access this part:
"location" : {
"lat" : 48.0418838,
"lng" : 14.4078882
},
with this:
$url1="http://maps.googleapis.com/maps/api/geocode/json?address=AT-".$a."&sensor=false";
$unparsed_json = file_get_contents($url1);
$data =(array) json_decode($unparsed_json);
echo $unparsed_json;
$latlng= $data['results']['location']['lat'];
echo $latlng;
but everything I try gives result null. Any help would be really appreciated.
You are using arrays in your json code and need to reference items accordingly, also your location is located in the geometry entity.
Try out with this $latlng= $data['results'][0]['geometry']['location']['lat'];
Here's even a demo in JS just to show the concept. Use the line above in your PHP code.
Related
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 want to have the State and county from an Address using geocode with Google API. This is my code:
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$street.''.$plz.''.$city.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$formatted_adress = $output->results[0]->formatted_address;
$state = $output->results[0]->address_components->administrative_area_level_2;
$county = $output->results[0]->address_components->administrative_area_level_1;
latitude, longitude and formatted_adress works find and I can save the results in my database, but the state and county doesn't work.
This is the output:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Hauptgasse",
"short_name" : "Hauptgasse",
"types" : [ "route" ]
},
{
"long_name" : "Solothurn",
"short_name" : "Solothurn",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Solothurn",
"short_name" : "Solothurn",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Solothurn",
"short_name" : "Südost",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Schweiz",
"short_name" : "CH",
"types" : [ "country", "political" ]
},
{
"long_name" : "4500",
"short_name" : "4500",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Hauptgasse, 4500 Solothurn, Schweiz",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 47.2088079,
"lng" : 7.540025399999999
},
"southwest" : {
"lat" : 47.2065432,
"lng" : 7.535295899999999
}
},
"location" : {
"lat" : 47.20778,
"lng" : 7.537360000000001
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 47.20902453029149,
"lng" : 7.540025399999999
},
"southwest" : {
"lat" : 47.20632656970849,
"lng" : 7.535295899999999
}
}
},
"partial_match" : true,
"place_id" : "ChIJS4BfGfnXkUcRqLUKnJzN54U",
"types" : [ "route" ]
}
],
"status" : "OK"
}
According to your json data that you show you need to get state and county like below:-
$state = $output->results[0]->address_components[2]->long_name;// i assume `Solothurn` is state name
$county = $output->results[0]->address_components[4]->long_name; // i assume `Schweiz` is county name
if county name also Solothurn then just change county code like below:-
$county = $output->results[0]->address_components[3]->long_name;
I'm using the Google Geocoding API, and PHP (cURL) to get the JSON array.
That's all working fine.
The problem is that not every output is the same.
Example:
I have two addresses:
Dam 1, Amsterdam
Kurfürstendamm 1, Berlin
For the Amsterdam address the count of $array['results'][0]['address_components'] is 8 and the one for Berlin 7.
JSON output:
Amsterdam:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "street_number" ]
},
{
"long_name" : "Dam",
"short_name" : "Dam",
"types" : [ "route" ]
},
{
"long_name" : "Centrum",
"short_name" : "Centrum",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Amsterdam",
"short_name" : "Amsterdam",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Amsterdam",
"short_name" : "Amsterdam",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Noord-Holland",
"short_name" : "NH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Nederland",
"short_name" : "NL",
"types" : [ "country", "political" ]
},
{
"long_name" : "1012 JS",
"short_name" : "1012 JS",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Dam 1, 1012 JS Amsterdam, Nederland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.3740904,
"lng" : 4.8951464
},
"southwest" : {
"lat" : 52.37303319999999,
"lng" : 4.893589
}
},
"location" : {
"lat" : 52.3735618,
"lng" : 4.8943677
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 52.37491078029149,
"lng" : 4.895716680291502
},
"southwest" : {
"lat" : 52.3722128197085,
"lng" : 4.893018719708498
}
}
},
"place_id" : "ChIJ46bYaccJxkcRR1k-7Pl25Kg",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
Berlin:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "street_number" ]
},
{
"long_name" : "Kurfürstendamm",
"short_name" : "Kurfürstendamm",
"types" : [ "route" ]
},
{
"long_name" : "Bezirk Charlottenburg-Wilmersdorf",
"short_name" : "Bezirk Charlottenburg-Wilmersdorf",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Duitsland",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "10719",
"short_name" : "10719",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kurfürstendamm 1, 10719 Berlin, Duitsland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.50440769999999,
"lng" : 13.3337799
},
"southwest" : {
"lat" : 52.5043971,
"lng" : 13.3337737
}
},
"location" : {
"lat" : 52.50440769999999,
"lng" : 13.3337737
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 52.50575138029149,
"lng" : 13.3351257802915
},
"southwest" : {
"lat" : 52.5030534197085,
"lng" : 13.3324278197085
}
}
},
"partial_match" : true,
"place_id" : "EixLdXJmw7xyc3RlbmRhbW0gMSwgMTA3MTkgQmVybGluLCBEZXV0c2NobGFuZA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
As you can see the country and postalcode indexes are different (country: 6 and 5, postalcode 7 and 6)
When I want to show the street, housenumber, postalcode, city and country for both cities something like this:
echo 'Street = '.$array['results'][0]['address_components'][1]['long_name'].'<br />';
echo 'Housenumber = '.$array['results'][0]['address_components'][0]['long_name'].'<br />';
echo 'Postalcode = '.$array['results'][0]['address_components'][7]['long_name'].'<br />';
echo 'City = '.$array['results'][0]['address_components'][3]['long_name'].'<br />';
echo 'Country = '.$array['results'][0]['address_components'][6]['long_name'].'<br />';
It shows the following output:
Amsterdam:
Street = Dam
Housenumber = 1
Postalcode = 1012 JS
City = Amsterdam
Country = Netherlands
Berlin:
Street = Kurfürstendamm
Housenumber = 1
Notice: Undefined offset: 7 in .... on line ....
Postalcode =
City = Berlin
Country = 10719
So the question is, how can I get the "long_name" per "types" in the "address_components" array?
Or maybe there are even better ways?
You could try the following code below to convert the JSON reponse to an array with the address components as keys:
function convertAddress($json)
{
$data = json_decode($json);
$address = array();
foreach($data['results']['address_components'] AS $_component){
$key = array_values($_component['types']);
$address[$key[0]] = $_component;
}
return $address;
}
This works great for me with your test data, you can then access data like so:
$address = convertAddress($berlin_address);
print $address['post_code']['long_name'];
Please note I have only worked on the address_components part of the JSON, the rest should be easy to work into the method though.
{
"results" : [
{
"address_components" : [
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "West Delhi",
"short_name" : "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" ]
}
],
"formatted_address" : "New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.88981589999999,
"lng" : 77.34181459999999
},
"southwest" : {
"lat" : 28.4010669,
"lng" : 76.8396999
}
},
"location" : {
"lat" : 28.635308,
"lng" : 77.22496
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.88981589999999,
"lng" : 77.34181459999999
},
"southwest" : {
"lat" : 28.4010669,
"lng" : 76.8396999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
How do i parse the above json string with php
Trying the following code out but no success:
$url = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=Delhi&sensor=true");
$json = json_decode($url);
foreach($json->results as $item) {
$location_item = array(
'address' => $item->formatted_address,
);
}
I am getting blank page the code doesn't work please help i am new to php
I am getting blank page the code doesn't work please help i am new to
php
Your code actually works , maybe you are not printing the data that is why you are getting a blank page.
Tested
<?php
$url = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=Delhi&sensor=true");
$json = json_decode($url);
foreach($json->results as $item) {
$location_item = array(
'address' => $item->formatted_address,
);
}
print_r($location_item);
OUTPUT :
Array
(
[address] => New Delhi, Delhi, India
)
I am not having any success getting values out of a JSON array object. I am specifically trying to get to geometry->location->lat
Here is what I am doing:
$url='http://maps.googleapis.com/maps/api/geocode/json?address='.$a.'&sensor=false';
$result = file_get_contents($url);
$jsonArr = json_decode($result,true);
echo "Lat: ".$jsonArr['results']['geometry']['location']['lat'];
Here is a print_r() of the JSON returned by Google:
{
"results" : [
{
"address_components" : [
{
"long_name" : "46814",
"short_name" : "46814",
"types" : [ "postal_code" ]
},
{
"long_name" : "Fort Wayne",
"short_name" : "Fort Wayne",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Indiana",
"short_name" : "IN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Fort Wayne, IN 46814, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 41.08472709999999,
"lng" : -85.26534090
},
"southwest" : {
"lat" : 41.00163510,
"lng" : -85.35540899999999
}
},
"location" : {
"lat" : 41.05472940,
"lng" : -85.30328109999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 41.08472709999999,
"lng" : -85.26534090
},
"southwest" : {
"lat" : 41.00163510,
"lng" : -85.35540899999999
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
The results are returned in an array. Get the first element and you're done. However you should make sure that you actually have at least one element in the results array.
echo "Lat: " . $jsonArr['results'][0]['geometry']['location']['lat'];