Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1028997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:28:28+00:00 2026-05-16T12:28:28+00:00

I have JavaScript function call to changeMapLocation function where in it I want to

  • 0

I have JavaScript function call to changeMapLocation function where in it I want to return the variables lat and long1. I have some problem in code to return those two variables and alert in function call.

var sample = changeMapLocation(state);
alert(sample.lat1);
alert(sample.lat2); 

function changeMapLocation(state) {
   var addressval=state;
   var address;
   var url;
   var googleUrl= "http://maps.google.com/maps/api/geocode/json?";
   var  sensor  = "&sensor=false";

   if(addressval != null && addressval !="" && addressval.length!=0) {

      address = "address="+encodeURIComponent(addressval);

      $.ajax({
        url:googleUrl+address+sensor,
        type:"POST",
        dataType:"json",
        success:function(longlatJson) {
           var jsonObj = JSON.parse(JSON.stringify(longlatJson));
           var lat = jsonObj.results[0].geometry.location.lat;
           var long1 = jsonObj.results[0].geometry.location.lng;
           alert(lat);
           alert(long1);
           //var latlng = new google.maps.LatLng(lat,long1);
           //alert(latlng);
        },
        error:function(){alert("unable to conect to google server");}
      });
   }
   return(lat1:lat,lat2:long1); 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:28:29+00:00Added an answer on May 16, 2026 at 12:28 pm

    You have a bigger problem in there. You are calling the asynchronous $.ajax() method, where its callback function will be called after your changeMapLocation() function returns, and therefore your function will not work as you are expecting. Follow the comments in the example below:

    function changeMapLocation(state) {
       var lat;
       var long1;
       // Since the $.ajax() method is using the asynchronous XMLHttpRequest, it 
       // will not block execution, and will return immediately after it is called,
       // without waiting for the server to respond.
       $.ajax({
          url: 'url-here',
          type: 'POST',
          success: function(longlatJson) {
             // The code here will be executed only when the server returns
             // a response to the ajax request. This may happen several 
             // milliseconds after $.ajax() is called.
             var jsonObj = JSON.parse(JSON.stringify(longlatJson));
             lat = jsonObj.results[0].geometry.location.lat;
             long1 = jsonObj.results[0].geometry.location.lng;
             // Now lat and long1 are set, but it is too late. Our 
             // changeMapLocation() function will have already returned.
          }
       });
       // This part will be reached before the server responds to the asynchronous
       // request above. Therefore the changeMapLocation() function returns an  
       // object with two properties lat1 and lat2 with an undefined value.
       return {lat1: lat, lat2: long1};
    }
    

    You should consider refactoring your code in such a way that the logic to handle the ajax response is in the success callback. Example:

    function changeMapLocation(state) {
       $.ajax({
          url: 'url-here',
          type: 'POST',
          success: function(longlatJson) {
             var jsonObj = JSON.parse(JSON.stringify(longlatJson));
             var lat = jsonObj.results[0].geometry.location.lat;
             var long1 = jsonObj.results[0].geometry.location.lng;
    
             // Update your map location in here, or call a helper function that
             // can handle it:
             myGoogleMap.setCenter(new google.maps.LatLng(lat, long1));
          }
       });
    }
    

    Note that changeMapLocation() does not return anything anymore. It will simply change the map location on its own, when the server responds to the Ajax request.


    In addition note that your lat and long1 variables were enclosed in the scope of the success inner function, and couldn’t be accessed from the outer function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to call c# function from javascript the code i have tried
I have this follow code in my javascript. I call this function when the
I have a problem when call applet method from javascript.. I used this function
Lets say I have a javascript function call moveItem which takes two parameters source
I have seen Javascript code that claims to speed up function call overhead like:
I have a showCustomersList() javascript function in FunctionBag.js now I want to call that
I Have Javascript Function and I Want to call it On each time user
I have a variable that I'm using to build a JavaScript function call, and
I have a javascript function from which I need to call a controller action
I would like to call JavaScript function many time . I have tried like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.