Link to home
Start Free TrialLog in
Avatar of dbasplus
dbasplusFlag 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?
ASKER CERTIFIED SOLUTION
Avatar of dbasplus
dbasplus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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