Jump to content

Recommended Posts

Posted

Hi,

 

could someone please guide or help to assign a js script to update edit fields for address input

 

Thank you

 

js script

----------

function Read(postcode)
{
   var country = 'GB'
   var urlfmt = 'https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:'+postcode;
   if(country != undefined)
     urlfmt += '|country:'+country;
   var content = web.Download(urlfmt);
   var obj = JSON.parse(content);
   var lat = obj.results[0].geometry.location.lat;
   var lng = obj.results[0].geometry.location.lng;

   var addrurl = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+ lat + ',' + lng + '&sensor=false'
   var addr = web.Download(addrurl);
   var addrObject = JSON.parse(addr);

   var data = GetAddressComponents(addrObject.results,postcode);
   if(data == null) return postcode;

   var street = ReadComponent('route',data);
   if(street == '-')
     street = ReadComponent('locality',data);
   if(street == '-')
     street = ReadComponent('administrative_area_level_4',data);
   var town = ReadComponent('postal_town',data);
   var county = ReadComponent('administrative_area_level_2',data);

   return postcode + ',' + street + ',' + town + ',' + county;
}

function GetAddressComponents(results,postcode)
{
    for(i=0;i < results.length;i++)
    {
       for(j=0; j< results.address_components.length;j++)
       {
         var component = results.address_components[j];
         if(component.long_name.replace(' ','') == postcode.replace(' ',''))
           return results.address_components;
       }
    }
    return null;
}

function ReadComponent(name,components)
{
   for(i=0;i<components.length;i++)
   {
      var component = components;
      
      for(j=0;j<component.types.length;j++)
      {
         if(component.types[j] == name)
           return component.long_name;
      }
   }
   
   return '-';
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...