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 8549421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:42:34+00:00 2026-06-11T13:42:34+00:00

I have the following code: /* * converts a string to geolocation and returns

  • 0

I have the following code:

/*
 * converts a string to geolocation and returns it
 */

function stringToLatLng(string){
    if(typeof string == "string"){
        geocoder = new google.maps.Geocoder();
        geocoder.geocode( { 'address': string}, function(results, status) {
           if (status == google.maps.GeocoderStatus.OK) {
              console.log("LatLng: "+results[0].geometry.location);
              return results[0].geometry.location;
           } else {
              console.log("Geocode was not successful for the following reason: " + status);
           }
        });
   }
}

the LatLng prints the correct location to the console, but when I write this:

var pos = stringToLatLng('New York');
            console.log(pos);

I get undefined back. Why is that? thanks

  • 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-06-11T13:42:35+00:00Added an answer on June 11, 2026 at 1:42 pm

    Something like this:

    function stringToLatLng(strloc, callback){
        if(typeof string == "string"){
            geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': strloc}, function(results, status) {
               if (status == google.maps.GeocoderStatus.OK) {
                  callback.call({}, results[0].geometry.location);
               } else {
                  console.log("Geocode was not successful for the following reason: " + status);
               }
            });
       }
    }
    
    stringToLatLng('New York', function(pos){
        console.log(pos);
    });
    

    In your code, when you return, you are actually returning from the function(results, status){..} function, not the stringToLatLng function, as said in the comments its an asynchronous call, so you must use a callback.

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

Sidebar

Related Questions

I have the following code that splits a string on newlines and converts it
I have the following code: Public Shared Function GetListAsString(ByVal data As List(Of String)) As
I have used following code to convert string to json and parse it. String
I have the following code that takes a double value and converts it to
I have a 'strange' problem, the following code converts the location lat value into
I have the following code, which uses a Unicode string class from a library
I have the following code IDictionary<string, IEnumerable<Control>> I need to convert it to IDictionary<string,
I have the following code: private ActionResult Foo(string format, IEnumerable<String> myResults) { if (format
I have the following code but it only converts strings to double. if the
I have the following code which will convert a string to TitleCase. I would

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.