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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:42:11+00:00 2026-05-24T20:42:11+00:00

First off thank you in advance for taking time to help me with this,

  • 0

First off thank you in advance for taking time to help me with this, I appreciate your efforts.

I have a problem with google maps api, JavaScript version 3.

I have written the following code

    $('.adr').ready(function(){    

    initialize();

})

function initialize() {

    var myLatlng = codeAddress();

    var myOptions = {
      zoom: 14,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);

  }

function codeAddress() 
{
    var geocoder = new google.maps.Geocoder();

    var address;
    var street = cropAdr($(".street-address").text());
    var city = cropAdr($(".locality").text());
    var state = cropAdr($(".region").text());
    var zip = cropAdr($(".zip").text()); 

    address = street + ", " + city + ", " + state + ", " + zip;    

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

      if (status == google.maps.GeocoderStatus.OK) 
      {
        var latlng = results[0].geometry.location;
        return latlng;
      }  
      else 
      {
        alert("Geocode was not successful for the following reason: " + status);        
        return null;
      }

    });
}



function cropAdr(args)
{
  var index = args.indexOf(":");
  var value = args.substr(index+1);

  return value;
}

But it doesn’t work.

I have checked the value of the “results[0].geometry.location” return and its perfect, so the address manipulation works. The “results[0].geometry.location” is a google.maps.Latlng object, but I have tried to strip out just the co-ords as strings, then create a new google.maps.Latlng object but no dice.

yet if I manually copy that string and paste the value into “var myLatlng = new google.maps.Latlng(Paste Copied String here!)” the whole thing works!!

I cannot see what else is wrong this script (apologies for the jumble of Jquery and Javascritpt).

  • 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-24T20:42:12+00:00Added an answer on May 24, 2026 at 8:42 pm

    The Google Maps API Geocoder accepts a function that will be run when the address has been geocoded, but that function may be called asynchronously – that is, after the rest of your code has already finished running.

    In codeAddress you call the Geocoder and pass in a function with this line:

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

    You then try and return a latLng from the function passed to the geocoder, but that is not the same as returning a value from codeAddress. The value you return from inside this function will be passed to the geocoder object, which will just ignore it.

    You need to have the function you pass to geocode do something with the latLng. For example, replace:

    return latLng;
    

    with:

    map.setCenter(latLng);
    

    And the map should center itself on the geocoded address once the result is available.

    (To do this you will need to make the map object global or otherwise make it available to codeAddress. I suggest adding “var map;” at the top of your code, and remove “var” from in front of the use of map in initialize)

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

Sidebar

Related Questions

First off, thanks in advance for your help. This issue is driving me nuts.
First off, let me thank you if you're taking the time to look at
first off this is a class assignment so i would appreciate help but just
First off I am new to this site and it is a big help,
First off, I'd like to apologize in advance for not knowing this. I've been
JAVA: First off, Thanks so much for taking the time to look at my
Let me start off by saying that this is my first time working with
First off thanks to all the users who have made my android developing adventure
first off I'm a noob to PHP but here is my problem. I am
First off I use this code to make the navigation bar always stay fixed;

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.