//For State fill autofill
$(document).on('blur','.auto',function(){
var address = $("input[name='address']").val();
address = strReplaceAll(address, ",", "");
clear();
if(address.length!=0){
$.ajax({
url : baseurl+'/APIS/Geo/'+address,
success : function(res){
var obj=JSON.parse(res);
$("#drpCountry option[value='"+obj.country.short_name+"']").prop('selected',true);
if(obj.state.long_name)
$('#txtState').val(obj.state.long_name);
if(obj.district.long_name)
$('#txtDistrict').val(obj.district.long_name);
if(obj.pincode)
$('#txtPincode').val(obj.pincode);
}
});
}
});
function strReplaceAll(string, Find, Replace){
try{
return string.replace( new RegExp(Find, "gi"), Replace );
}catch(ex){return string;}
}
function clear(){
$("#drpCountry option[value='IN']").prop('selected',true);
$('#txtState').val("");
$('#txtDistrict').val("");
$('#txtPincode').val("");
}
<?php
class Geo extends CI_Controller{
public function _remap($address){
$this->index($address);
}
public function index($address){
/* Usage: In my case, I needed to return the State and Country of an address */
$myLocation = $this->reverse_geocode($address);
echo json_encode($myLocation);
}
function reverse_geocode($address) {
$address = str_replace(" ", "+", "$address");
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents("$url");
$json = json_decode($result);
$city=$district=$state=$country=$pincode=null;
foreach ($json->results as $result) {
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = array('long_name'=>$addressPart->long_name, 'short_name'=>$addressPart->short_name);
else if((in_array('administrative_area_level_2',$addressPart->types)) && (in_array('political', $addressPart->types)))
$district = array('long_name'=>$addressPart->long_name, 'short_name'=>$addressPart->short_name);
else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = array('long_name'=>$addressPart->long_name, 'short_name'=>$addressPart->short_name);
else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = array('long_name'=>$addressPart->long_name, 'short_name'=>$addressPart->short_name);
else if((in_array('postal_code', $addressPart->types))){
$pincode=$addressPart->long_name;
}
}
}
$addArr=array('city'=>$city,'district'=>$district,'state'=>$state,'country'=>$country,'pincode'=>$pincode);
return $addArr;
}
function api(){
}
}
?>
0 comments:
Post a Comment