You are viewing limited content. For full access, please sign in.

Question

Question

Can Forms populate the Address fields like Google Maps?

asked on March 2, 2016

We have a Customer request to populate the "Address" form fields using auto fill just like Google Maps?

I believe their is a Google places API, but no idea how to get started with this in Forms.

Can this be done on the "Address" field or would you have to create Single line fields and populate these?

A simple step by step instructions would be great if anyone has done this?

 

cheers

Grant

 

 

 

0 0

Answer

SELECTED ANSWER
replied on March 3, 2016

First you'll need to get an API key from Google for the Google Maps JavaScript API.

Then, make a backup copy of _FormLayout.cshtml under your forms directory /Forms/Views/Form and open the original in a text editor. Find the line that has json2.js script, then add this line immediately after it:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>

Remember to plug in your API key.

Restart your Forms App Pool or IIS. Then, in Forms, you can turn a single line field to a dynamic address picker simply by doing:

$(document).ready(function() {

    var autocomplete = new google.maps.places.Autocomplete(
        (document.getElementById('Field7')),
        {types: ['geocode']});

});

Change Field7 to be the id of your own single line field.

Here's a short video of the final result.

If you really want, you can then take the address the user picks and populate a Forms address field's sections with it. You can look at this tutorial by Google and repurpose it for your needs.

1 0

Replies

replied on March 3, 2016 Show version history

If you are using Forms 10 you can easily do this. Create a new form and place an Address Field Widget on to it. Now click on the "CSS and Javascript" tab and place the following code in the JavaScript area:

 

$.cachedScript = function( url, options ) {
  options = $.extend( options || {}, {
    dataType: "script",
    cache: true,
    url: url
  });
  
  return $.ajax( options );
};

//https://ubilabs.github.io/geocomplete/
var gUrl = "https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places";
var gcUrl = "https://ubilabs.github.io/geocomplete/jquery.geocomplete.js";

$.cachedScript(gUrl).done(
  function(){
    $.cachedScript(gcUrl).done(
  function( script, textStatus ) {

    //#Field1_DSG0 is the ID for the Street Address input on the Address widget
  $("#Field1_DSG0").geocomplete
            ({ details: "form", detailsAttribute: "class" }).bind(
              "geocode:result", 
              function(event, result){
                
                //https://developers.google.com/maps/documentation/geocoding/intro?csw=1#Types
                var ac = result.address_components;
                var c = {};
                $.each(ac, function(k,v1) {$.each(v1.types, function(k2, v2){c[v2]=v1.short_name});})
                
                var lo = c.locality;
                if(lo === undefined)
                  lo = c.postal_town;
                //#Field1_DSG2 is the ID for the City input on the Address widget
                $("#Field1_DSG2").val(lo);
                
                var aal = c.administrative_area_level_1;
                if(aal === undefined)
                  aal = c.administrative_area_level_2;
                
                //#Field1_DSG3 is the ID for the State / Province / Region input on the Address widget
                $("#Field1_DSG3").val(aal);
                
                var pc = c.postal_code;
                if(c.postal_code_suffix != undefined)
                  pc += "-" + c.postal_code_suffix;
                
                //#Field1_DSG4 is the ID for the Postal / Zip Code input on the Address widget
                $("#Field1_DSG4").val(pc);
                
                //#Field1_DSG5 is the ID for the Country input on the Address widget
                $("#Field1_DSG5").val(c.country);
                
                //Reformat Street Address to only have the address in it
                var addr = $("#Field1_DSG0").val().split(",");
                $("#Field1_DSG0").val(addr[0]);
                
                //console.log(result);
              });
});
});

Just ensure that "#Field1_DSG0" is the actual ID of the Address input box (Most likely it will be but you may want to view the source of the form to make sure). I have included the links in the code for further reading on the different variables that are returned depending on your country of origin.

 

UPDATE: Special thanks to Melissa Ward for this - You will need to obtain an API Key from Google and replace the YOUR_API_KEY text in the Google Places Map URL above with the one assigned to you. More information about obtaining a Google Map API can be found at https://developers.google.com/maps/documentation/javascript/get-api-key

Save the form and run it.

 

This method doesn't require a google key nor any recompiling.

 

Hope this helps!

Wes

8 0
replied on March 3, 2016

You're right about the API key. I forgot they removed that requirement. smiley

1 0
replied on April 6, 2018

Hey! I'm working with a client who is using this script. Internally on their personal computers, this works perfectly. However, I attempt to test on the server or attempt to fill out the form externally the address will no longer populate. On the server once I enter any information the street address field greys out and an exclamation mark appears in the corner of the field. Externally, nothing happens. I have to manually enter the address. Any advice on how I can correct this? 

0 0
replied on April 6, 2018

I fiddled around with this a little bit more and found that replacing the part in the code that has "https://maps.googleapis.com/maps/api/js?libraries=places" with "https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places" replacing the YOUR API KEY with the key from https://developers.google.com/maps/documentation/javascript/get-api-key did the trick, thanks.

2 0
replied on April 24, 2018

I have updated the code to reflect the changes by Melissa. Thank you!

2 0
replied on December 13, 2018

Hi

Thanks for the script helped me, I was just wondering if there are any changes to be made if I wanted to populate the address with a Place and not the address location.

 

I would appreciate some help.


Thank you

0 0
replied on January 10, 2022

Trying to get this to run where the Address field is within a collection.

I have tried $("#Field14(1)_DSG0").val(addr[0]);  as an example

I have also tried $("#Field14\(1\)_DSG0").val(addr[0]);

Where the (1) is the first in the collection.

0 0
replied on January 11, 2022

Jonathan,

 I believe you are going to have to figure out how to bind the autocomplete whenever someone adds a new address field to the collection. Your example seems to me that you are trying to bind to it before the address field is even created which will not work.

 

Wesley Funderberg

0 0
replied on January 11, 2022

Hello Wesley,

I have the collections set to 2 sets only. The user cannot add more sets.

I am not familiar with autocomplete. But I can do some research in to that.

0 0
replied on July 4, 2022

Is it possible to restrict the Country with this script? I have looked at the "jquery.geocomplete.js" and even copied it to my local server. I can't figure out what to change. It's a monster of a script :)

 

 

0 0
replied on February 28, 2023

Hi Wes - we've been using your script successfully for a while in Classic Designer - but just wondering if you (or anyone else) has managed to get it to work in the New Designer?

We've tried simply converting the Classic form into the New version and then copy/pasting the JS into the New Designer code area (making sure field references are the same) - but this doesn't seem to work.

Is the JS still not 'complete enough' in New Designer?

Thanks.

0 0
replied on March 6, 2023

Duncan,

I have not even tried due to the lack of support of JS in the new designer. Just know that if I do figure out how to accomplish it, I will effeminately post it here.

 

Thanks!

2 0
replied on March 6, 2023

Excellent - thanks! I'll stay subscribed yes

0 0
replied on April 4, 2023

How can this code be applied when you need to enter more than one address inside a collection object?
Since the address field ID changes incrementally each time you add a new address. For example:

Field1(1)_DSG0, Field1(2)_DSG0, Field1(3)_DSG0, etc...

How can I modify this code so that in cases like these I can apply it to each of those IDs that auto-increment within the collection field?

0 0
replied on April 4, 2023

Hi Mark,

When the Address field is within a collection, you can do this...

$("#Field14\\(1\\)_DSG2").val(lo);

Let me know if you need more of this JavaScript :)

1 0
replied on April 11, 2023

What you suggest worked perfectly, but I have another problem, it is only enabled when there is only one address, but when I start to add more address blocks, the script for the others does not load, excuse my lack of knowledge of javascript, but there will be some How to do it so that when another is added the code is loaded?

I will show you how I try to use it in case there are more address collection blocks. In this specific case, it is only intended to collect up to 3 blocks of addresses, but I have cases that depend on the user, how many they need,
Do you have any suggestion?

I put you here how I tried to use the code

 

//Get Adress from Google Map APIkey

$.cachedScript = function( url, options ) {
  options = $.extend( options || {}, {
    dataType: "script",
    cache: true,
    url: url
  });
  
  return $.ajax( options );
};
//https://ubilabs.github.io/geocomplete/
var gUrl = "https://maps.googleapis.com/maps/api/js?key=AIzaSyByBy72OACZHy8jBwwruJb08LjfchBz9-Q&libraries=places";
var gcUrl = "https://ubilabs.github.io/geocomplete/jquery.geocomplete.js";

$.cachedScript(gUrl).done(
  function(){
    $.cachedScript(gcUrl).done(
  function( script, textStatus ) {

    //#Field1_DSG0 is the ID for the Street Address input on the Address widget $("#Field14\\(1\\)_DSG2")
  $("#Field15\\(1\\)_DSG0").geocomplete
            ({ details: "form", detailsAttribute: "class" }).bind(
              "geocode:result", 
              function(event, result){
                
                //https://developers.google.com/maps/documentation/geocoding/intro?csw=1#Types
                var ac = result.address_components;
                var c = {};
                $.each(ac, function(k,v1) {$.each(v1.types, function(k2, v2){c[v2]=v1.short_name});})
                
                var lo = c.locality;
                if(lo === undefined)
                  lo = c.postal_town;
                //#Field1_DSG2 is the ID for the City input on the Address widget
                $("#Field15\\(1\\)_DSG2").val(lo);
                
                var aal = c.administrative_area_level_1;
                if(aal === undefined)
                  aal = c.administrative_area_level_2;
                
                //#Field1_DSG3 is the ID for the State / Province / Region input on the Address widget
                $("#Field15\\(1\\)_DSG3").val(aal);
                
                var pc = c.postal_code;
                if(c.postal_code_suffix != undefined)
                  pc += "-" + c.postal_code_suffix;
                
                //#Field1_DSG4 is the ID for the Postal / Zip Code input on the Address widget
                $("#Field15\\(1\\)_DSG4").val(pc);
                
                //#Field1_DSG5 is the ID for the Country input on the Address widget
                $("#Field15\\(1\\)_DSG5").val(c.country);
                
                //Reformat Street Address to only have the address in it
                var addr = $("#Field15\\(1\\)_DSG0").val().split(",");
                $("#Field15\\(1\\)_DSG0").val(addr[0]);
                
                //console.log(result);
              });
});
});
//Get Adress from Google Map APIkey

$.cachedScript = function( url, options ) {
  options = $.extend( options || {}, {
    dataType: "script",
    cache: true,
    url: url
  });
  
  return $.ajax( options );
};
//https://ubilabs.github.io/geocomplete/
var gUrl = "https://maps.googleapis.com/maps/api/js?key=AIzaSyByBy72OACZHy8jBwwruJb08LjfchBz9-Q&libraries=places";
var gcUrl = "https://ubilabs.github.io/geocomplete/jquery.geocomplete.js";

$.cachedScript(gUrl).done(
  function(){
    $.cachedScript(gcUrl).done(
  function( script, textStatus ) {

    //#Field1_DSG0 is the ID for the Street Address input on the Address widget $("#Field14\\(1\\)_DSG2")
  $("#Field15\\(2\\)_DSG0").geocomplete
            ({ details: "form", detailsAttribute: "class" }).bind(
              "geocode:result", 
              function(event, result){
                
                //https://developers.google.com/maps/documentation/geocoding/intro?csw=1#Types
                var ac = result.address_components;
                var c = {};
                $.each(ac, function(k,v1) {$.each(v1.types, function(k2, v2){c[v2]=v1.short_name});})
                
                var lo = c.locality;
                if(lo === undefined)
                  lo = c.postal_town;
                //#Field1_DSG2 is the ID for the City input on the Address widget
                $("#Field15\\(2\\)_DSG2").val(lo);
                
                var aal = c.administrative_area_level_1;
                if(aal === undefined)
                  aal = c.administrative_area_level_2;
                
                //#Field1_DSG3 is the ID for the State / Province / Region input on the Address widget
                $("#Field15\\(2\\)_DSG3").val(aal);
                
                var pc = c.postal_code;
                if(c.postal_code_suffix != undefined)
                  pc += "-" + c.postal_code_suffix;
                
                //#Field1_DSG4 is the ID for the Postal / Zip Code input on the Address widget
                $("#Field15\\(2\\)_DSG4").val(pc);
                
                //#Field1_DSG5 is the ID for the Country input on the Address widget
                $("#Field15\\(2\\)_DSG5").val(c.country);
                
                //Reformat Street Address to only have the address in it
                var addr = $("#Field15\\(2\\)_DSG0").val().split(",");
                $("#Field15\\(2\\)_DSG0").val(addr[0]);
                
                //console.log(result);
              });
});
});
//Get Adress from Google Map APIkey

$.cachedScript = function( url, options ) {
  options = $.extend( options || {}, {
    dataType: "script",
    cache: true,
    url: url
  });
  
  return $.ajax( options );
};
//https://ubilabs.github.io/geocomplete/
var gUrl = "https://maps.googleapis.com/maps/api/js?key=AIzaSyByBy72OACZHy8jBwwruJb08LjfchBz9-Q&libraries=places";
var gcUrl = "https://ubilabs.github.io/geocomplete/jquery.geocomplete.js";

$.cachedScript(gUrl).done(
  function(){
    $.cachedScript(gcUrl).done(
  function( script, textStatus ) {

    //#Field1_DSG0 is the ID for the Street Address input on the Address widget $("#Field14\\(1\\)_DSG2")
  $("#Field15\\(3\\)_DSG0").geocomplete
            ({ details: "form", detailsAttribute: "class" }).bind(
              "geocode:result", 
              function(event, result){
                
                //https://developers.google.com/maps/documentation/geocoding/intro?csw=1#Types
                var ac = result.address_components;
                var c = {};
                $.each(ac, function(k,v1) {$.each(v1.types, function(k2, v2){c[v2]=v1.short_name});})
                
                var lo = c.locality;
                if(lo === undefined)
                  lo = c.postal_town;
                //#Field1_DSG2 is the ID for the City input on the Address widget
                $("#Field15\\(3\\)_DSG2").val(lo);
                
                var aal = c.administrative_area_level_1;
                if(aal === undefined)
                  aal = c.administrative_area_level_2;
                
                //#Field1_DSG3 is the ID for the State / Province / Region input on the Address widget
                $("#Field15\\(3\\)_DSG3").val(aal);
                
                var pc = c.postal_code;
                if(c.postal_code_suffix != undefined)
                  pc += "-" + c.postal_code_suffix;
                
                //#Field1_DSG4 is the ID for the Postal / Zip Code input on the Address widget
                $("#Field15\\(3\\)_DSG4").val(pc);
                
                //#Field1_DSG5 is the ID for the Country input on the Address widget
                $("#Field15\\(3\\)_DSG5").val(c.country);
                
                //Reformat Street Address to only have the address in it
                var addr = $("#Field15\\(3\\)_DSG0").val().split(",");
                $("#Field15\\(3\\)_DSG0").val(addr[0]);
                
                //console.log(result);
              });
});
});

 

addresscollector.png
0 0
replied on March 27

Hi, now I'm trying to do the same in Laserfiche 11, inside the Modern Design Form version, and doesn't work. Any idea how to make it functional for the Modern Design Forms Address field?

 

Thank You

0 0
replied on March 2, 2016

I know I've hooked up the Google Maps API to Laserfiche Forms before and used it exactly in the manner you describe, but I think I had to use single line fields.

We hardly if ever use the Address fields in forms. They look nice, but their lack of flexibility (e.g. being able to choose which subfields should be used) make them more trouble than they're worth.

2 0
replied on April 24, 2017

Ege,

I am trying to implement something similar for my form. I am using Forms 10 and I don't see json2.script in _FormLayout.cshtml. So I ended up adding the script in the forms css and javascript itself. But this doesn't work for me. 

$(document).ready(function() {

 $.getScript("http://maps.googleapis.com/maps/api/js?v=3&key=APIKey&libraries=places");
 
var autocomplete = new google.maps.places.Autocomplete(
        (document.getElementById('Field1')),

        {types: ['geocode']});

 
});  


Can you help please

0 0
replied on April 24, 2017

Vineela,

 Did you replace APIKey with your actual key from Google along with changing the element ID?

 

Wes

0 0
replied on April 24, 2017

Yes, I did replaced with my own APIKey

0 0
replied on March 3, 2016

Very much appreciated Ege and Wez for your replies. yes

1 0
replied on August 15, 2018

Any updates on this in 10.2?  I'm struggling to make this work.  Thanks!

1 0
replied on March 2, 2016

Thanks Ege for your reply, are you able to provide any details of how you did this?

 

cheers

 

0 0
replied on April 26, 2018

I am also looking for a way to do this with single line fields as the address fields can cause problems in other areas and we avoid using collections if at all possible. I made a test form with the address fields and used the code listed above which worked awesome....but my client wants it done with single line fields instead.

Does anyone have instructions on how to make that work with single line fields?

Thank you in advance :) 

0 0
replied on April 10, 2019

Just replace the address field variables ie. #Field1_DSG0 with single line field variable ie. #Field25. I have also had one geo search fill the address field and a single line field by separating them with a comma ("#Field1_DSG0, #Field25")

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.