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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:16:48+00:00 2026-06-15T08:16:48+00:00

I am trying to use the google maps javascript API to map an address

  • 0

I am trying to use the google maps javascript API to map an address based on this example.

https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

The documentation recommends the clientside javascript approach as the best way to deal with quotas on requests. So far so good. My problem is in moving from this example to my specific case. My addresses are already in a database so I don’t need the user to enter one. Also I do not want the map to load with the page. Instead, I want the map for the address to load when the user clicks a link.

I have a script working that loads the map in a div using initialize (). But my problem is getting initialize to work with geocode. The geocode in the example depends on initialize loading with bodyonload which I do not want.

Here is code. Would appreciate any suggestions:

javascript

    var map;
    var geocoder;


     function codeAddress() {
            var address = document.getElementById('address').value;
            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
              } else {
                alert('Geocode was not successful for the following reason: ' + status);
              }
            });
          }

      function initialize() {
    geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(40.7562008,-73.9903784);
        var myOptions = {
          zoom: 18,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      }

html

<input id="address" type="hidden" value="Palo Alto CA">
<a href="javascript:void(0)" onclick="initialize()">View map without geocoding</a>
<div id="map_canvas" style="width:300px; height:300px;"></div>
<a href="javascript:void(0)" onclick="codeAddress()">View map of geocoded address</a>
  • 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-15T08:16:49+00:00Added an answer on June 15, 2026 at 8:16 am

    The only issue I had with your script was the following line in the initialize() function:

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

    By declaring var map, your script is just declaring a local variable named map, as opposed to using the global map variable declared at the top of your script.

    By removing var, the script uses the global variable and runs fine:

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

    Finally, to get the geocoded map to load on the link click, change onclick for your geocoded address to onclick="initialize();codeAddress();".

    Added:

    Try combining your initialize() and codeAddress() methods into the following:

    function initialize() {
        geocoder = new google.maps.Geocoder();
    
        var address = document.getElementById('address').value;
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var myOptions = {
                    zoom: 18,
                    center: results[0].geometry.location,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    

    And then just call initialize() from your link.

    Basically, what we’re doing is taking the call to geocoder.geocode() that codeAddress() was performing and inside the resulting delegate, we’re using results[0].geometry.location to initialize the map. This way, the temporary latlong doesn’t need to be displayed.

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

Sidebar

Related Questions

I'm trying to use this: https://developers.google.com/maps/documentation/javascript/places#places_autocomplete . How do I control the width of
I'd like to use jQuery with Google maps. I'm trying to replicate this example
I'm trying to show a custom map using the Google Maps Javascript API. In
I'm trying to use the Google maps JavaScript API to draw a line between
I'm trying to use Google Maps API and i'm stuck here: I need an
I'm experiencing a problem while trying to use google maps api v3 (with marker
I've created a map using google maps v3 api for GWT. I'm trying to
I'm trying to use the Google Maps V3 API to create markers on a
Am trying to use google.loader.ClientLocation along with the maps api to get the location
I'm trying to replicate the following exemple : http://www.leigeber.com/2008/04/map-your-users-using-the-google-maps-api-and-php/ How to integrate this PHP

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.