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

Question

Question

Retrieve latitude and longitude of a google map address

asked on February 22, 2023

I am currently using Google Maps to autocomplete an address. I found that solution in this forum: https://answers.laserfiche.com/questions/93304/Can-Forms-populate-the-Address-fields-like-Google-Maps

I am now trying to retrieve the latitude and longitude of the above address. I have read some Google Maps documentation and have gotten lost. I don't want to display a google map. I only want the lat/long of the address that was entered. 

 

$.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);
              });
});

0 0

Replies

replied on February 22, 2023

Adding this to your existing JavaScript should get you started. It fills a Single Line field with the Latitude and Longitude values. 

var LatLong = result.geometry;
                
                $("#Field9").val(LatLong.location).trigger('change');

0 0
replied on February 22, 2023

Thank you! This works, can I get the values separated? 

0 0
replied on February 22, 2023

This should work...

var LatLong = result.geometry;
                
                $("#Field9").val(LatLong.location).trigger('change');
                var splitlatlong = $("#Field9").val().split(",");
                $("#Field12").val(splitlatlong[0]);
                $("#Field13").val(splitlatlong[1]);

 

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

Sign in to reply to this post.