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'];
}
Related
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'];
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.
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"];
}
}
{
"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
)