Avatar of dbasplus
dbasplus
Flag for Australia asked on

Incorrect item used in java sub-function for Maps

I have the following scripts that create markers on a Google map.

  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
  <script type="text/javascript">
      function ShowAddresses(Addresses, PropertyIDs) {
          var myOptions = {
              zoom: 14,
              mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var geocoder = new google.maps.Geocoder();
          var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
          var bounds = new google.maps.LatLngBounds();
          var myArray = Addresses.split(";");
          var IDs = PropertyIDs.split(";");
          for (var i = 0; i < myArray.length; i++) {
              var address = myArray[i];
              var propertyID = IDs[i];
              geocoder.geocode({ 'address': address }, function (results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                      map.setCenter(results[0].geometry.location);
                      alert(propertyID);
                      var marker = new google.maps.Marker({
                          position: results[0].geometry.location,
                          title: results[0].formatted_address + "(" + propertyID + ")",
                          animation: google.maps.Animation.DROP,
                          url: "PropertyDetails.aspx?prop_i=" + propertyID,
                          map: map
                      });
                      google.maps.event.addListener(marker, "click", function () {
                          window.location = marker.url;
                      });
                      bounds.extend(results[0].geometry.location);
                  } else {
                      //alert("Geocode was not successful for the following reason: " + status);
                  }
              });
          }
      }
  </script>

Open in new window


I pass in delimited lists for address and property ID (these come from the database behind). I want to use the property id to reference the local page when clicked.

My issue is that the 'propertyID' used in the 'geocoder.geocode' is always the last item in the list. What is the best way to get the current propertyID to be used?
.NET ProgrammingJava

Avatar of undefined
Last Comment
dbasplus

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dbasplus

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dbasplus

ASKER
Another solution that removes a variable:

  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
  <script type="text/javascript">
      function ShowAddresses(Addresses, PropertyIDs) {
          var myOptions = {
              zoom: 14,
              mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var geocoder = new google.maps.Geocoder();
          var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
          var bounds = new google.maps.LatLngBounds();
          var myArray = Addresses.split(";");
          var IDs = PropertyIDs.split(";");
          for (var i = 0; i < myArray.length; i++) {
              var address = myArray[i];
              (function (propertyID) {
                  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({
                              position: results[0].geometry.location,
                              title: results[0].formatted_address,
                              animation: google.maps.Animation.DROP,
                              url: "PropertyDetails.aspx?prop_i=" + propertyID,
                              map: map
                          });
                          google.maps.event.addListener(marker, "click", function () {
                              window.location = marker.url;
                          });
                          bounds.extend(results[0].geometry.location);
                      } else {
                          //alert("Geocode was not successful for the following reason: " + status);
                      }
                  });
              })(IDs[i]);
          }
      }
  </script>

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck