Make this Function into your Model in Codeigniter.
function zipcode($addr){
if(!empty($addr)){
//Address format
$formattedaddress = str_replace(' ','+',$addr);
//Send request and receive data by address through Json
$mapcodeFromaddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedaddr.'&sensor=true_or_false');
$outcome1 = json_decode($mapcodeFromaddr);
//Get latitude and longitute data through Json
$lat = $outcome1->results[0]->geometry->location->lat;
$long = $outcome1->results[0]->geometry->location->long;
//Send request and receive json data by latitude longitute
$mapcodeFromlatlong = getfile_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng='.$latitude.','.$long.'&sensor=true_or_false');
$outcome2 = json_decode($mapcodeFromlatlong);
if(!empty($outcome2)){
$addrcomponents = $outcome2->results[0]->addr_components;
foreach($addrcomponents as $addrcomp){
if($addrcomp->types[0] == 'postal_code'){
//Return the zipcode
return $addrcomp->long_name;
}
}
return false;
}else{
return false;
}
}
else{
return false;
}
}
If you want to use this Google API Key in your code it will helps you how?. If your daily request limit exceed then you would not get any result this Api Key will helps you .
$mapcodeFromaddr = getfile_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedaddr.'&sensor=true_or_false&key=GoogleAPIKey');
0 comments:
Post a Comment