How to get location value from json value? - php

First i use this link for get longtitude and longtitude from city name
https://maps.googleapis.com/maps/api/geocode/json?address=usa,califonia,Los%20Angeles&key={API-KEY}&language=EN
...........................................................................................................................................................
When load this url i get this json format.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Los Angeles",
"short_name" : "Los Angeles",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Los Angeles County",
"short_name" : "Los Angeles County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Los Angeles, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.3373061,
"lng" : -118.1552891
},
"southwest" : {
"lat" : 33.7036519,
"lng" : -118.6681759
}
},
"location" : {
"lat" : 34.0522342,
"lng" : -118.2436849
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.3373061,
"lng" : -118.1552891
},
"southwest" : {
"lat" : 33.7036519,
"lng" : -118.6681759
}
}
},
"place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
I want to get location ==> lat = 34.0522342 and location ==> lng = -118.2436849 from json format.
Then i use this code.
<?php
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=usa,califonia,Los%20Angeles&key={API-KEY}&language=EN';
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$location_lat = $json_data['results']['location']['lat'];
$location_lng = $json_data['results']['location']['lng'];
echo $location_lat;
echo $location_lng;
?>
But i not get any data ? How can i do ?

If you get the layers of data correct, then you should use...
$location_lat = $json_data['results'][0]['geometry']['location']['lat'];
$location_lng = $json_data['results'][0]['geometry']['location']['lng'];
echo $location_lat.PHP_EOL;
echo $location_lng;
Which outputs...
34.0522342
-118.2436849

you can edit to
$location_lat = $json_data['results'][0]['geometry']['location']['lat'];
$location_lng = $json_data['results'][0]['geometry']['location']['lng'];

Related

How to get a fields particular value of JSON in Codeigniter Php?

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'];
}

Get State and County from Address

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;

Get the right values from a JSON array using Google's Geocoding API

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.

Access Values in a JSON Array PHP

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'];

geocoder json to get street address, city, country ,postcode

I do have requirement to pull out
street name, postcode, city, state, country , latitude, longitude
from my address ..
Geocode API returns this in JSON format my web browser
what is the most common format to use (long_name or short_name)
{
"results" : [
{
"address_components" : [
{
"long_name" : "City Road",
"short_name" : "City Rd",
"types" : [ "route" ]
},
{
"long_name" : "Darlington",
"short_name" : "Darlington",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New South Wales",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Australia",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2008",
"short_name" : "2008",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "City Road, Darlington NSW 2008, Australia",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -33.88775940,
"lng" : 151.19316880
},
"southwest" : {
"lat" : -33.8913630,
"lng" : 151.18843910
}
},
"location" : {
"lat" : -33.88924140,
"lng" : 151.19051430
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : -33.88775940,
"lng" : 151.19316880
},
"southwest" : {
"lat" : -33.8913630,
"lng" : 151.18843910
}
}
},
"types" : [ "route" ]
}
],
"status" : "OK"
}
This is my code
$address = urlencode($address);
$googleApi = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';
$json = file_get_contents(sprintf($googleApi, $address));
$resultObject = json_decode($json);
$address = $resultObject->results[0]->address_components;
// please help me in pulling other data
$location = $resultObject->results[0]->geometry->location;
$latitude = $location->lat;
$longitude = $location->lng;
// (latitude and longitude is working)
$location = $resultObject->results[0]->address_components[0];
$long_name = $location->long_name;// long_name not ong_name
$short_name = $location->short_name;

Categories