I want to echo value in the json array one by one when i want
<?php
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false // don't return headers
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$response = get_web_page("http://maps.googleapis.com/maps/api/geocode/json?address=colombo");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>";
print_r($resArr);
echo "</pre>";
echo "<br>";
echo "<br>";
;
?>
This is the current result in the browser above code
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Colombo
[short_name] => Colombo
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Colombo
[short_name] => Colombo
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Western Province
[short_name] => WP
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Sri Lanka
[short_name] => LK
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Colombo, Sri Lanka
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 6.9805844
[lng] => 79.8900852
)
[southwest] => stdClass Object
(
[lat] => 6.8625113
[lng] => 79.8225192
)
)
[location] => stdClass Object
(
[lat] => 6.9270786
[lng] => 79.861243
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 6.9805844
[lng] => 79.8900852
)
[southwest] => stdClass Object
(
[lat] => 6.8625113
[lng] => 79.8225192
)
)
)
[place_id] => ChIJA3B6D9FT4joRjYPTMk0uCzI
[types] => Array
(
[0] => locality
[1] => political
)
)
)
[status] => OK
)
I want to echo value by value
try it with:
$resArr = json_decode($response, true);
It will convert it into associative array format.
For more detail have a look at JSON Decode PHP
Related
I would like to get country, pincode,state and city name from longitude and latitude over Google maps API - with PHP. but sometimes it works correct, but sometime it gets wrong information and many times it returns null values because the result comes is dynamic not fixed.
so anybody suggest me how to make this dynamic and check inside is country city pincode and state are available in any of the array index from result ....
below is my code...
$fromlat = $this->request->data['Order']['from_lat'];
$fromlong = $this->request->data['Order']['from_long'];
$geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$fromlat.','.$fromlong.'&sensor=false');
$json = json_decode($geocode);
//print_r($geocode);
$country = $json->{'results'}[1]->{'address_components'}[3]->{'long_name'};
$state = $json->{'results'}[1]->{'address_components'}[2]->{'long_name'};
$city = $json->{'results'}[1]->{'address_components'}[1]->{'long_name'};
$locality = $json->{'results'}[1]->{'address_components'}[0]->{'long_name'};
$pincode = $json->{'results'}[1]->{'address_components'}[4]->{'long_name'};
echo $locality."</br>".$country."</br>".$state."</br>".$city."</br>".$pincode;
i need to check types in address components like below in result forloop and if available then display result.
types = country = country name;
types = administrative_area_level_1 = state name ;
types = administrative_area_level_2 = city name ;
types = postal_code = pincode ;
below is my result array...
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Kurkheda - Wadsa - Bramhapuri - Nagpur Highway
[short_name] => MH MSH 9
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Kurkheda - Wadsa - Bramhapuri - Nagpur Hwy, Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0633475
[lng] => 79.1652473
)
[southwest] => stdClass Object
(
[lat] => 21.0599518
[lng] => 79.1646723
)
)
[location] => stdClass Object
(
[lat] => 21.0616013
[lng] => 79.1647236
)
[location_type] => GEOMETRIC_CENTER
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0633475
[lng] => 79.166308780291
)
[southwest] => stdClass Object
(
[lat] => 21.0599518
[lng] => 79.163610819708
)
)
)
[place_id] => ChIJj5wNX6O51DsRz53GLG9aZRM
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Kalamana
[short_name] => Kalamana
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Kalamana, Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0628156
[lng] => 79.164412
)
[southwest] => stdClass Object
(
[lat] => 21.0589309
[lng] => 79.1617083
)
)
[location] => stdClass Object
(
[lat] => 21.0605174
[lng] => 79.1629662
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.0628156
[lng] => 79.164412
)
[southwest] => stdClass Object
(
[lat] => 21.0589309
[lng] => 79.1617083
)
)
)
[place_id] => ChIJ2yhznKO51DsRGZWqNrtKBD4
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => 441204
[short_name] => 441204
[types] => Array
(
[0] => postal_code
)
)
[1] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maharashtra 441204, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.139709
[lng] => 79.3452018
)
[southwest] => stdClass Object
(
[lat] => 20.8714046
[lng] => 79.0962536
)
)
[location] => stdClass Object
(
[lat] => 20.9903768
[lng] => 79.2451149
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.139709
[lng] => 79.3452018
)
[southwest] => stdClass Object
(
[lat] => 20.8714046
[lng] => 79.0962536
)
)
)
[place_id] => ChIJKTHfbpOw1DsRGwYyMATAfU4
[types] => Array
(
[0] => postal_code
)
)
[3] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Nagpur
[short_name] => Nagpur
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Nagpur, Maharashtra, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.713785
[lng] => 79.65179
)
[southwest] => stdClass Object
(
[lat] => 20.5841799
[lng] => 78.2515599
)
)
[location] => stdClass Object
(
[lat] => 21.3170198
[lng] => 79.1999949
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 21.713785
[lng] => 79.65179
)
[southwest] => stdClass Object
(
[lat] => 20.5841799
[lng] => 78.2515599
)
)
)
[place_id] => ChIJ691n-cTA1DsRh3HGEikxZ2E
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Maharashtra
[short_name] => MH
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maharashtra, India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 22.027909
[lng] => 80.890924
)
[southwest] => stdClass Object
(
[lat] => 15.602412
[lng] => 72.6593629
)
)
[location] => stdClass Object
(
[lat] => 19.7514798
[lng] => 75.7138884
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 22.027909
[lng] => 80.890924
)
[southwest] => stdClass Object
(
[lat] => 15.602412
[lng] => 72.6593629
)
)
)
[place_id] => ChIJ-dacnB7EzzsRtk_gS5IiLxs
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => India
[short_name] => IN
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => India
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 35.5087008
[lng] => 97.395561
)
[southwest] => stdClass Object
(
[lat] => 6.7535159
[lng] => 68.1623859
)
)
[location] => stdClass Object
(
[lat] => 20.593684
[lng] => 78.96288
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 35.5087008
[lng] => 97.395561
)
[southwest] => stdClass Object
(
[lat] => 6.7535159
[lng] => 68.1633666
)
)
)
[place_id] => ChIJkbeSa_BfYzARphNChaFPjNc
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
Proper way to extract values of address components is done as following :
if (results[0]) {
$("input[name=lat]").val(results[0].geometry.location.lat());
$("input[name=lng]").val(results[0].geometry.location.lng());
var addressArr = [];
$.each(results[0].address_components, function (key, val) {
if (!$.inArray("country", results[0].address_components[key].types)) {
addressArr['country'] = results[0].address_components[key].long_name;
}
if (!$.inArray("administrative_area_level_1", results[0].address_components[key].types)) {
addressArr['state'] = results[0].address_components[key].long_name;
}
if (!$.inArray("administrative_area_level_2", results[0].address_components[key].types)) {
addressArr['city'] = results[0].address_components[key].long_name;
}
});
console.log(addressArr);
}
}
I have some similar code that is known to work. Basically it can return a results array if there are more than one results possible or just an object if only one result is found. My use-case is slightly different, but hopefully it helps. Try this:
$ch = curl_init();
$geoCodeUrl = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $zip . ',+United+States&key=' . $googleApiKey;
curl_setopt($ch,CURLOPT_URL, $geoCodeUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
$GeoCode = json_decode($result);
$GeoLocator = new \stdClass();
if( isset($GeoCode->results[0]) ){
$GeoLocator->lat = $GeoCode->results[0]->geometry->location->lat;
$GeoLocator->lon = $GeoCode->results[0]->geometry->location->lng;
foreach($GeoCode->results[0]->address_components as $comp){
if( in_array('locality',$comp->types) ){
$GeoLocator->city = trim($comp->short_name);
}
else if( in_array('administrative_area_level_1',$comp->types) ){
$GeoLocator->state = strtoupper(trim(preg_replace('/\d/', '', $comp->short_name)));
}
}
}
else if( isset($GeoCode->results) && isset($GeoCode->results->geometry) ){
$GeoLocator->lat = $GeoCode->results->geometry->location->lat;
$GeoLocator->lon = $GeoCode->results->geometry->location->lng;
foreach($GeoCode->results->address_components as $comp){
if( in_array('locality',$comp->types) ){
$GeoLocator->city = trim($comp->short_name);
}
else if( in_array('administrative_area_level_1',$comp->types) ){
$GeoLocator->state = strtoupper(trim(preg_replace('/\d/', '', $comp->short_name)));
}
}
}
I am using PHP to convert a list of postcodes + County into latitude and longitude. I have tried using the google map api and the simplexml_load_file() function like so:
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=WS9+8NS,+West+Midlands&sensor=false";
$result = simplexml_load_file($url);
print"<pre>";
print_r($result);
Now this works fine as I am able to get the following:
SimpleXMLElement Object
(
[status] => OK
[result] => SimpleXMLElement Object
(
[type] => postal_code
[formatted_address] => Walsall, Walsall, West Midlands WS9 8NS, UK
[address_component] => Array
(
[0] => SimpleXMLElement Object
(
[long_name] => WS9 8NS
[short_name] => WS9 8NS
[type] => postal_code
)
[1] => SimpleXMLElement Object
(
[long_name] => Walsall
[short_name] => Walsall
[type] => Array
(
[0] => locality
[1] => political
)
)
[2] => SimpleXMLElement Object
(
[long_name] => Walsall
[short_name] => Walsall
[type] => postal_town
)
[3] => SimpleXMLElement Object
(
[long_name] => West Midlands
[short_name] => West Mids
[type] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => SimpleXMLElement Object
(
[long_name] => United Kingdom
[short_name] => GB
[type] => Array
(
[0] => country
[1] => political
)
)
)
[geometry] => SimpleXMLElement Object
(
[location] => SimpleXMLElement Object
(
[lat] => 52.6030230
[lng] => -1.9175368
)
[location_type] => APPROXIMATE
[viewport] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 52.6013713
[lng] => -1.9188871
)
[northeast] => SimpleXMLElement Object
(
[lat] => 52.6040692
[lng] => -1.9161892
)
)
[bounds] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 52.6020654
[lng] => -1.9182516
)
[northeast] => SimpleXMLElement Object
(
[lat] => 52.6033751
[lng] => -1.9168247
)
)
)
)
)
Problem now is, how should I take out the first latitude / longitude and not the whole thing, I only need these two values.
Please note that the lat/long are dynamic as the address changes for each search.
From the print_r you can see the structure of the content. Then, it is a matter of following it.
This contains the values:
$result->result->geometry->location
So to print them separately, you can say:
print "lat: " . $result->result->geometry->location->lat;
print "lng: " . $result->result->geometry->location->lng;
All together:
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=WS9+8NS,+West+Midlands&sensor=false";
$result = simplexml_load_file($url);
print "lat: " . $result->result->geometry->location->lat . " <br/>";
print "lng: " . $result->result->geometry->location->lng . " <br/>";
Better to use their JSON API.
$url ="http://maps.googleapis.com/maps/api/geocode/json?address=WS9+8NS,+West+Midlands&sensor=false";
$result = file_get_contents($url);
print"<pre>";
$resultArray = json_decode($result, true);
print_r($result);
Then just access like an array!
e.g.
$resultArray['results']['geometry']['location']['lat']
$resultArray['results']['geometry']['location']['lng']
I would use cURL instead of file get contents as it gives more control. But this works fine.
I would also check the $resultArray before attempting to do anything with it.
I want to know the place name by Lat / Lng. For that I found Google API. The program I write is
<?php
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=13.74349%2C100.489067&sensor=false';
$data = json_decode(file_get_contents($url));
echo '<pre>';
print_r($data);
?>
The output is given below
Array
(
[0] => stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Wang Doem 8
[short_name] => Wang Doem 8
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[long_name] => Wat Arun
[short_name] => Wat Arun
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
[5] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Wang Doem 8, Wat Arun, Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7448616
[lng] => 100.4888865
)
[southwest] => stdClass Object
(
[lat] => 13.7445018
[lng] => 100.4876895
)
)
[location] => stdClass Object
(
[lat] => 13.7446807
[lng] => 100.4882883
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7460306803
[lng] => 100.48963698
)
[southwest] => stdClass Object
(
[lat] => 13.7433327197
[lng] => 100.48693902
)
)
)
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Wat Arun
[short_name] => Wat Arun
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Wat Arun, Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.7361885
[lng] => 100.4803822
)
)
[location] => stdClass Object
(
[lat] => 13.7434901
[lng] => 100.4890647
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.7361885
[lng] => 100.4803822
)
)
)
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.722343
[lng] => 100.4615641
)
)
[location] => stdClass Object
(
[lat] => 13.7341183
[lng] => 100.4761897
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.722343
[lng] => 100.4615641
)
)
)
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[3] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
[1] => stdClass Object
(
[long_name] => Wat Tha Phra
[short_name] => Wat Tha Phra
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Wat Tha Phra, Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.5127718
)
[southwest] => stdClass Object
(
[lat] => 13.694656
[lng] => 100.4615641
)
)
[location] => stdClass Object
(
[lat] => 13.7247046
[lng] => 100.4958678
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.5127718
)
[southwest] => stdClass Object
(
[lat] => 13.694656
[lng] => 100.4615641
)
)
)
[types] => Array
(
[0] => postal_code
)
)
[4] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Bangkok, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
[location] => stdClass Object
(
[lat] => 13.7522222
[lng] => 100.4938889
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
)
[types] => Array
(
[0] => locality
[1] => political
)
)
[5] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Bangkok, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
[location] => stdClass Object
(
[lat] => 13.7234186
[lng] => 100.4762319
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
)
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[6] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 20.465143
[lng] => 105.636812
)
[southwest] => stdClass Object
(
[lat] => 5.612851
[lng] => 97.343396
)
)
[location] => stdClass Object
(
[lat] => 15.870032
[lng] => 100.992541
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 20.465143
[lng] => 105.636812
)
[southwest] => stdClass Object
(
[lat] => 5.612851
[lng] => 97.343396
)
)
)
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
)
I don't know how to grab the address_components at the top. I try with $data[0]->results[0]->address_components and etc. But it is not working. Please check this once... Thank you.
The following works for me:
$data->results[0]->address_components[0]->long_name
You might also want to enable json_decode's assoc-parameter so that objects get converted to associative arrays.
E.g.
$data = json_decode(file_get_contents($url), true);
$data["results"][0]["address_components"][0]["long_name"]
On my page I have a number of registered users, each user has a postcode stored in their row in my table.
I'm wondering if its possible and if so how to get the postcode of my website visitors and then show them the nearest users going of the data in my table?
You can use HTML 5 Geolocation to ask them to provide their location to you. Then you could lookup what the nearest postal code is based on their longitude/latitude.
You can get free postal/zip data with long/lat for North America here:
http://geocoder.ca/?freedata=1
If you can determine the coordinates of the visiting user in an accurate way you could use the google reverse geocoding API to translate the coordinates to an address and use the zipcode from that address.
Determining accurate coordinates of a visitor could prove difficult though. You'll have to rely on a GeoLocation api like http://www.maxmind.com/app/php or do an implementation of the HTML5 GeoLocation API.
You need to populate latitude/longitude for each record, then use Google Maps to find out nearest users, I believe following links will help you out.
Get latitude and longitude based on location name with Google Autocomplete API
Google Maps Api v3 - find nearest markers
Interesting, building on what others have said, perhaps this example will help ;)
<?php
//example a google IP
$user_info = get_ip_info('74.125.224.72');
$google_info = get_google_data($user_info['latitude'],$user_info['longitude']);
$user = array_merge($user_info,$google_info);
//See below for result
print_r($user);
function get_google_data($long,$lat){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$long.','.$lat.'&sensor=true');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data,true);
}
function get_ip_info($ip = NULL){
if(empty($ip)) $ip = $_SERVER['REMOTE_ADDR'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.ipaddresslocation.org/ip-address-locator.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,array('ip'=>$ip));
$data = curl_exec($ch);
curl_close($ch);
preg_match_all('/<i>([a-z\s]+)\:<\/i>\s+<b>(.*)<\/b>/im',$data,$matches,PREG_SET_ORDER);
if(count($matches)==0)return false;
$return = array();
$labels = array(
'Hostname' => 'host',
'IP Country' => 'country',
'IP Country Code' => 'country_code',
'IP Continent' => 'continent',
'IP Region' => 'region',
'IP Latitude' => 'latitude',
'IP Longitude' => 'longitude',
'Organization' => 'organization',
'ISP Provider' => 'isp');
foreach($matches as $info){
if(isset($info[2]) && !is_null($labels[$info[1]])){
$return[$labels[$info[1]]]=$info[2];
}
}
return (count($return))?$return:false;
}
/**
* Result:
*
*
* Array
(
[host] => nuq04s07-in-f8.1e100.net
[country] => United States
[country_code] => USA
[continent] => North America
[region] => California
[latitude] => 37.4192
[longitude] => -122.0574
[organization] => Google
[isp] => Google
[results] => Array
(
[0] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Sevryns Rd
[short_name] => Sevryns Rd
[types] => Array
(
[0] => route
)
)
[1] => Array
(
[long_name] => Mountain View
[short_name] => Mountain View
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => Array
(
[long_name] => Santa Clara
[short_name] => Santa Clara
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[3] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[4] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
[5] => Array
(
[long_name] => 94043
[short_name] => 94043
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Sevryns Rd, Mountain View, CA 94043, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.4197948
[lng] => -122.0577433
)
[southwest] => Array
(
[lat] => 37.4175854
[lng] => -122.0590077
)
)
[location] => Array
(
[lat] => 37.4187254
[lng] => -122.0583059
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.420039080292
[lng] => -122.05702651971
)
[southwest] => Array
(
[lat] => 37.417341119709
[lng] => -122.05972448029
)
)
)
[types] => Array
(
[0] => route
)
)
[1] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Moffett Federal Airfield
[short_name] => Moffett Federal Airfield
[types] => Array
(
[0] => establishment
)
)
[1] => Array
(
[long_name] => Cummins Ave
[short_name] => Cummins Ave
[types] => Array
(
[0] => route
)
)
[2] => Array
(
[long_name] => Mountain View
[short_name] => Mountain View
[types] => Array
(
[0] => locality
[1] => political
)
)
[3] => Array
(
[long_name] => Santa Clara
[short_name] => Santa Clara
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
[6] => Array
(
[long_name] => 94043
[short_name] => 94043
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Moffett Federal Airfield (NUQ), Cummins Ave, Mountain View, CA 94043, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.4298225
[lng] => -122.0375869
)
[southwest] => Array
(
[lat] => 37.4019577
[lng] => -122.0589996
)
)
[location] => Array
(
[lat] => 37.4199527
[lng] => -122.0584702
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.4298225
[lng] => -122.0375869
)
[southwest] => Array
(
[lat] => 37.4019577
[lng] => -122.0589996
)
)
)
[types] => Array
(
[0] => airport
[1] => transit_station
[2] => establishment
)
)
[2] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => 94043
[short_name] => 94043
[types] => Array
(
[0] => postal_code
)
)
[1] => Array
(
[long_name] => Mountain View
[short_name] => Mountain View
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Mountain View, CA 94043, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.464087
[lng] => -122.03599
)
[southwest] => Array
(
[lat] => 37.3857439
[lng] => -122.10842
)
)
[location] => Array
(
[lat] => 37.428434
[lng] => -122.0723816
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.464087
[lng] => -122.03599
)
[southwest] => Array
(
[lat] => 37.3857439
[lng] => -122.10842
)
)
)
[types] => Array
(
[0] => postal_code
)
)
[3] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => Santa Clara
[short_name] => Santa Clara
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[1] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[2] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Santa Clara, CA, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 37.484637
[lng] => -121.208178
)
[southwest] => Array
(
[lat] => 36.894155
[lng] => -122.202476
)
)
[location] => Array
(
[lat] => 37.2938907
[lng] => -121.7195459
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 37.484637
[lng] => -121.208178
)
[southwest] => Array
(
[lat] => 36.894155
[lng] => -122.202476
)
)
)
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => California
[short_name] => CA
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => California, USA
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 42.0095169
[lng] => -114.131211
)
[southwest] => Array
(
[lat] => 32.5342071
[lng] => -124.4096195
)
)
[location] => Array
(
[lat] => 36.778261
[lng] => -119.4179324
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 42.0095169
[lng] => -114.131211
)
[southwest] => Array
(
[lat] => 32.5342071
[lng] => -124.4096195
)
)
)
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => United States
[short_name] => US
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => United States
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 90
[lng] => 180
)
[southwest] => Array
(
[lat] => -90
[lng] => -180
)
)
[location] => Array
(
[lat] => 37.09024
[lng] => -95.712891
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 49.38
[lng] => -66.94
)
[southwest] => Array
(
[lat] => 25.82
[lng] => -124.39
)
)
)
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
*/
?>
I am trying to extract the postal code from the Google Geocode v3 API XML Response. The difficulty I am having is that sometimes the postal_code value is contained in the 6th address_element and sometimes in the 7th. Here is the XML response that I am working with and in this sample response you can see that the postal_code is the 6th element of the address_component array:
SimpleXMLElement Object
(
[status] => OK
[result] => Array
(
[0] => SimpleXMLElement Object
(
[type] => street_address
[formatted_address] => 177 Church St, Toronto, ON M5C 2G5, Canada
[address_component] => Array
(
[0] => SimpleXMLElement Object
(
[long_name] => 177
[short_name] => 177
[type] => street_number
)
[1] => SimpleXMLElement Object
(
[long_name] => Church St
[short_name] => Church St
[type] => route
)
[2] => SimpleXMLElement Object
(
[long_name] => Toronto
[short_name] => Toronto
[type] => Array
(
[0] => locality
[1] => political
)
)
[3] => SimpleXMLElement Object
(
[long_name] => Toronto
[short_name] => Toronto
[type] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => SimpleXMLElement Object
(
[long_name] => Ontario
[short_name] => ON
[type] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => SimpleXMLElement Object
(
[long_name] => Canada
[short_name] => CA
[type] => Array
(
[0] => country
[1] => political
)
)
[6] => SimpleXMLElement Object
(
[long_name] => M5C 2G5
[short_name] => M5C 2G5
[type] => postal_code
)
)
[geometry] => SimpleXMLElement Object
(
[location] => SimpleXMLElement Object
(
[lat] => 43.6547363
[lng] => -79.3763746
)
[location_type] => RANGE_INTERPOLATED
[viewport] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 43.6533849
[lng] => -79.3777345
)
[northeast] => SimpleXMLElement Object
(
[lat] => 43.6560829
[lng] => -79.3750365
)
)
[bounds] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 43.6547315
[lng] => -79.3763964
)
[northeast] => SimpleXMLElement Object
(
[lat] => 43.6547363
[lng] => -79.3763746
)
)
)
)
I haven't tried this, but I think it should work.
$address = $array->result[0]->address_component;
$postal_code = '';
foreach($address as $adr){
if($adr->type == 'postal_code'){
$postal_code = $adr->long_name;
}
}