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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:08:17+00:00 2026-05-13T15:08:17+00:00

I have tried to create an application which creates a marker on the map

  • 0

I have tried to create an application which creates a marker on the map on user’s click. Then if user clicks a marker an address should be displayed.

Trying to accomplish this I encountered a problem and so far haven’t found a solution. When I click on the map for the first time the first marker is created. Then when I try to click at that marker nothing appears. Then on second click a second marker appears and if I click on second marker an address which should be displayed at the first marker appears on the second and so on. So the marker addresses are somehow displaced and can’t figure out what’s the problem.

I have been looking for an issue but cannot determine what causes such strange behavior. I am a newbie in JavaScript so I don’t know whether it is somehow related with asynchronous request or there is some error in the code I overlooked.

The code is the following:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">

</script>
<script type="text/javascript">
    var map;
    var counter;
    var latlng;
    var locationAddress;
    var geocoder;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    latlng = new google.maps.LatLng(46.043830, 14.488864);
    var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    counter = 0;

     google.maps.event.addListener(map, 'click', function(event) {
        map.setCenter(event.latlng);
        codeLatLng(event.latLng);
    placeMarker(event.latLng);
  });
  }

  function placeMarker(location) {
  var clickedLocation = new google.maps.LatLng(location);
  latlng = location;
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });

  addLocationInfo(marker);
}

function addLocationInfo(marker){
    var infoWindow = new google.maps.InfoWindow({content: locationAddress, size: new google.maps.Size(50, 50)});
    google.maps.event.addListener(marker, 'click', function(){
        infoWindow.open(map, marker);
    });
}

function codeLatLng(latlng) {
    if (geocoder) {
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            locationAddress = results[1].formatted_address;
          }
        } else {
          locationAddress = "Neznan naslov";
        }
      });
    }
  }


</script>

I would be very thankful if anyone could point out what is the cause of the issue or suggest any better solution.

Thank you!

  • 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-13T15:08:18+00:00Added an answer on May 13, 2026 at 3:08 pm

    The problem is that the geocoder is asynchronous and so the locationAddress gets its value after you have added the infowindow with and undefined content..

    The addLocationInfo will have to be called from inside the callback function of the geocoder..
    Following is the code with some minor changes in the order of called functions (and some parameters to them) to make this happen..

       var map;
        var counter;
        var latlng;
        var locationAddress;
        var geocoder;
      function initialize() {
        geocoder = new google.maps.Geocoder();
        latlng = new google.maps.LatLng(46.043830, 14.488864);
        var myOptions = {
          zoom: 16,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        counter = 0;
    
         google.maps.event.addListener(map, 'click', function(event) {
            map.setCenter(event.latlng);
            placeMarker(event.latLng);
    
      });
      }
    
      function placeMarker(location) {
      var clickedLocation = new google.maps.LatLng(location);
      latlng = location;
      var marker = new google.maps.Marker({
          position: location, 
          map: map
      });
      codeLatLng(location, marker);
    }
    
    function addLocationInfo(marker){
        var infoWindow = new google.maps.InfoWindow({content: locationAddress, size: new google.maps.Size(50, 50)});
        google.maps.event.addListener(marker, 'click', function(){
            infoWindow.open(map, marker);
        });
    }
    
    function codeLatLng(latlng, marker) {
        if (geocoder) {
          geocoder.geocode({'latLng': latlng}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[1]) {
                locationAddress = results[1].formatted_address;
              }
            } else {
              locationAddress = "Neznan naslov";
            }
            addLocationInfo(marker);
          });
        }
      }
    
    • 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.