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

  • Home
  • SEARCH
  • 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 1019443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:00:50+00:00 2026-05-16T11:00:50+00:00

im having a little problem with the Google API maps, i was wondering if

  • 0

im having a little problem with the Google API maps, i was wondering if anyone could help. I am trying to get infowindows to open with some HTML in containing a HREF. Now i could use the “X” to close the window however id prefer a second method of a Javascript timer to close the info windows as shown below..

setTimeout(‘infowindow.close(map,marker)’,’2000′);

The browser however reports that info window does not exist after the 2 seconds expire. To fix the problem i attempted to add infowindow object and marker objects to an array in global scope, however this did not resolve the issue..

I’ve returned the code to its original state would would be grateful for any advise on the issue..

<!DOCTYPE html> 
    <html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title></title> 
    <link href="http://code.google.com/apis/maps/documentation/javascript/examples/standard.css" rel="stylesheet" type="text/css" /> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 

    var geocoder;
    var map;
    var markers = [];
    var info = [];
    var i = 0;

    function initialize()
    {
       geocoder = new google.maps.Geocoder();
       var latlng = new google.maps.LatLng(40.768505, -111.853244);
       var myOptions =
       {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
       }
       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
       addPostCode('bb9 5pl','HTML');
       addPostCode('bb9 7de','HTML');
       addPostCode('bb12 7nq','HTML');
       addPostCode('bb4 6be','HTML');
    }

    function addPostCode(zip, html)
    {
       geocoder.geocode(
       {
          'address': zip
       }, 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,
                name: zip
             });

            var infowindow = new google.maps.InfoWindow({
            content: html
            });

            google.maps.event.addListener(marker, 'mouseover', function() 
            { 
              infowindow.open(map,marker); 
              setTimeout('infowindow.close(map,marker)','2000'); 
            });


            i++;
          }
          else
          {
             alert("Geocode was not successful for the following reason: " + status);
          }
       });
    }

    function checkZip(zip)
    {
       var distance = Number.MAX_VALUE;
       var index = 0;
       geocoder.geocode(
       {
          'address': zip
       }, function (results, status)
       {
          if (status == google.maps.GeocoderStatus.OK)
          {
             for (ix = 0; ix < markers.length; ix++)
             {
                var tmp = getDistance(results[0].geometry.location, markers[ix].position);
                if (tmp < distance)
                {
                   distance = tmp;
                   index = ix;
                }
             }
             alert('nearest zipcode is :' + markers[index].name + ' Distance:' + distance) 
          }
       });
    }

    function getDistance(latlng1, latlng2)
    {
       var R = 6371; // Radius of the earth in km 
       var dLat = (latlng2.lat() - latlng1.lat()) * Math.PI / 180; // Javascript functions in radians 
       var dLon = (latlng2.lng() - latlng1.lng()) * Math.PI / 180;
       var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(latlng1.lat() * Math.PI / 180) 
               * Math.cos(latlng2.lat() * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
       var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
       var d = R * c; // Distance in km 
       d = d * 0.621371192;
       return d;
    }

    </script> 
    </head> 
    <body onload="initialize()"> 
      <div> 
        <input id="address" type="textbox" value=""> 
        <input type="button" value="Geocode" onclick="checkZip(getElementById('address').value)"> 
      </div> 
    <div id="map_canvas" style="height:500px; width: 600px;"></div> 
    </body> 
    </html> 
  • 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-16T11:00:51+00:00Added an answer on May 16, 2026 at 11:00 am

    infowindow in your setTimeout call is out of scope when it triggers. Change that call to the following to correct this using Closures:

    setTimeout(function(){infowindow.close();}, '2000');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.