I am using google maps API in OpenLayers for geocoding so that when I enter an address I can get back the coordinates of this address. I am implementing this with a function but I cannot get the return values outside of the scope of the function. The implementation is the following:

function googleGeocoding(start){    

            var geocoder = new google.maps.Geocoder();

            geocoder.geocode({address:start}, function(results, status){

                var x1 = results[0].geometry.location.lat();            

                var y1 = results[0].geometry.location.lng();                

                alert(x1+' '+y1);  <--- this one alerts 

            alert(x1+' '+y1);   <-- this one gives back undefined 



How can I get the values of x1, y1 outside of the scope of geocoding? I'm sorry if this is more of JavaScript related question than GIS


Firstly, x1 and y1 are scoped to the callback, thus you cannot get their values unless they are defined out of the scope or globally.

Secondly, even though if you define them globally, their values won't be ready until the callback function is called.

If you have multiple controls calling the function googleGeocoding, then you can either send the ID of the control or use Sender object to get knowledge about the control requesting the value, check below example:


<div id=lstCoordinates >
  <input type=text onchange=googleGeocoding(this) />
  <input type=text onchange=googleGeocoding(this) />
  <input type=text onchange=googleGeocoding(this) />


  function googleGeocoding(sender) {
      var geocoder = new google.maps.Geocoder();
      var ctrl = $(this); //current input control
          address: $(this).val()
      }, function (results, status) {
          var x1 = results[0].geometry.location.lat();
          var y1 = results[0].geometry.location.lng();
          // do whatever code to notify the current input control that you got its values
          ctrl.attr('data-x', x1);
          ctrl.attr('data-y', y1);

          // Check if all inputs are populated
          if ($('#lstCoordinates').find('input:not([data-x])').length == 0)

  function CalculateAll() {
      // get all inputs with coordinates stored in 'data-x' and 'data-y' attributes
      var inputsCoordinates = $('#lstCoordinates').find('input');
  • so.. any suggestion ?
    – Stefanos
    Dec 10 '17 at 22:14
  • Can you explain why you need them out of scope? I think once you got the value of x1 and y1 and want to do some action like showing the value to the user or trigger other action, then 100% you will add the code inside the callback where you got the values.
    – Shiko
    Dec 10 '17 at 22:21
  • well these values are coming from a text input. I would like to get more values from another text input and then all together use them in another part of the script. Like: first click , first values are coming back. Second click second pair of values are coming back. Then take all the responses and use them elsewhere.
    – Stefanos
    Dec 10 '17 at 22:27
  • @Stefanos I have updated my answer, it is one of the solutions. another one is to use the input control ID instead.
    – Shiko
    Dec 10 '17 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.