3

My main goal with the program in question is to generate a Google map that shows the location of a specific building.

Due to Google limitation reasons, I have generated and stored all latitudes and longitudes for all the buildings that I analyze in a MS SQL database(it is a real estate web site). Everytime one building is selected then I retreive its corresponding latitud and longitude and store it in two asp:Label's. I use a script in Javascript in order to process the latitud and longitud which are passed on via two asp:Label's. My problem is that for some reason the LatLng function does not seem to work property and my maps do not show the coordinates that they should. I think I may have a problem with the type of variable that LatLng is expecting. I have tried both, default string that is passed on and converting the variables to real type. Here is the script. Any help or suggestions are appreciated:

  <script type="text/javascript">
      (function () {

          // Defining global variables
          var map, geocoder, marker, infowindow, propertyaddress, selectedbuilding, maplatitude, maplongitude, buildinglatlng, latlng, myOptions;

          function InitializeMap() {
              //propertyaddress = '400 Alton Road, Miami Beach, FL 33139';
              propertyaddress = document.getElementById('<%=lblselectedHiddenBuildingAddress.ClientID%>').innerText;
              selectedbuilding = document.getElementById('<%=lblMainBuilding.ClientID%>').innerText;

              //maplatitude = parseFloat(document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText);
              //maplongitude = parseFloat(document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText);
              maplatitude = document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText;
              maplongitude = document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText;
              buildinglatlng = new google.maps.LatLng(maplatitude, maplongitude);

              //window.alert("Processed propertyaddress");

              //latlng = new google.maps.LatLng(25.76804, -80.132743);

              // Creating an object literal containing the properties
              // we want to pass to the map
               myOptions = {
                  zoom: 15,
                  center: new google.maps.LatLng(maplatitude, maplongitude),
                  //center: buildinglatlng,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  scaleControl: true,
                  streetViewControl: true,
                  disableDefaultUI: true,
                  mapTypeControl: true,
                  mapTypeControlOptions: {
                      style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                      position: google.maps.ControlPosition.TOP_LEFT,
                      mapTypeIds: [
                google.maps.MapTypeId.ROADMAP,
                google.maps.MapTypeId.TERRAIN,
                google.maps.MapTypeId.SATELLITE
            ]
                  },
                  navigationControl: true,
                  navigationControlOptions: {
                      position: google.maps.ControlPosition.TOP_LEFT
                  }
              };
              // Creating the map
              map = new google.maps.Map(document.getElementById("map"), myOptions);
          }
          window.onload = InitializeMap;
      })();
</script>
#

Added Code that Works but uses the geocoder

#

For instance the following code works perfectly but it uses the geocoder. Passing the variables is not a problem. There is something strange with LatLng and what it does with the variables passed. It does get the values with all the significant places though.

  <script type="text/javascript">
      (function () {

          // Defining global variables
          var map, geocoder, marker, infowindow, propertyaddress, selectedbuilding, maplatitude, maplongitude, buildinglatlng, latlng, myOptions;

          function InitializeMap() {
              //propertyaddress = '400 Alton Road, Miami Beach, FL 33139';
              propertyaddress = document.getElementById('<%=lblselectedHiddenBuildingAddress.ClientID%>').innerText;
              selectedbuilding = document.getElementById('<%=lblMainBuilding.ClientID%>').innerText;

              //maplatitude = parseFloat(document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText);
              //maplongitude = parseFloat(document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText);
              maplatitude = document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText;
              maplongitude = document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText;
              buildinglatlng = new google.maps.LatLng(maplatitude, maplongitude);

              //window.alert("Processed propertyaddress");

              //latlng = new google.maps.LatLng(25.76804, -80.132743);

              if (!geocoder) {
                  geocoder = new google.maps.Geocoder();
              }

              // Creating a GeocoderRequest object
              var geocoderRequest = {
                  address: propertyaddress
              }


              geocoder.geocode(geocoderRequest, function (results, status) {
                  // Check if status is OK before proceeding
                  if (status == google.maps.GeocoderStatus.OK) {
                      // Center the map on the returned location
                      //map.setCenter(results[0].geometry.location);
                      // Creating an object literal containing the properties
                      // we want to pass to the map
                      myOptions = {
                          zoom: 15,
                          //center: new google.maps.LatLng(maplatitude, maplongitude),
                          center: results[0].geometry.location,
                          mapTypeId: google.maps.MapTypeId.ROADMAP,
                          scaleControl: true,
                          streetViewControl: true,
                          disableDefaultUI: true,
                          mapTypeControl: true,
                          mapTypeControlOptions: {
                              style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                              position: google.maps.ControlPosition.TOP_LEFT,
                              mapTypeIds: [
                                google.maps.MapTypeId.ROADMAP,
                                google.maps.MapTypeId.TERRAIN,
                                google.maps.MapTypeId.SATELLITE
                                ]
                          },
                          navigationControl: true,
                          navigationControlOptions: {
                              position: google.maps.ControlPosition.TOP_LEFT
                          }
                      };
                      // Creating the map
                      map = new google.maps.Map(document.getElementById("map"), myOptions); 

                      // Check to see if we've already got a Marker object
                      if (!marker) {
                          // Creating a new marker and adding it to the map
                          marker = new google.maps.Marker({
                              map: map,
                              animation: google.maps.Animation.DROP
                          });
                          google.maps.event.addListener(marker, 'click', toggleBounce);
                      }
                      // Setting the position of the marker to the returned location
                      marker.setPosition(results[0].geometry.location);
                      // Check to see if we've already got an InfoWindow object

                      google.maps.event.addListener(marker, 'click', function () {
                          if (!infowindow) {
                              // Creating a new InfoWindow
                              infowindow = new google.maps.InfoWindow();
                          }
                          // Creating the content of the InfoWindow to the address
                          // and the returned position
                          var content = '<h2>' + selectedbuilding + '</h2>';
                          //content += 'Lat: ' + results[0].geometry.location.lat() + '<br />';
                          //content += 'Lng: ' + results[0].geometry.location.lng();
                          // Adding the content to the InfoWindow
                          infowindow.setContent(content);
                          // Opening the InfoWindow
                          infowindow.open(map, marker);
                      });

                      // Triggering the click event
                      google.maps.event.trigger(marker, 'click');
                  };
              });



          }

          function toggleBounce() {
              if (marker.getAnimation() != null) {
                  marker.setAnimation(null);
              } else {
                  marker.setAnimation(google.maps.Animation.BOUNCE);
              }
          }

          window.onload = InitializeMap;
      })();
</script>

Regards, Elias

  • 1
    The problem might be with your usage of `innerText` (see http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox). What happens if you alert `maplatitude` and `maplongitude`? – Tim M. Nov 29 '12 at 14:00
  • No it is not innerText as I am passing the address and for instance the following code works perfectly, but I will get charged a lot for using the geocoder: – Elias Echeverri Nov 29 '12 at 16:54

2 Answers2

0

Have you verified that maplatitude and maplongitude contain the values you expect (eg. with a debugger)?

Is there any correlation between the expected location and the actual location shown on the map (for example, if the building is at N21.7684, and the marker is placed at N21.0000, the decimal places may have been lost).

fbitterlich
  • 882
  • 7
  • 24
  • Yes, I have debugged the hell out of it and have traced the fact that LatLng contains what it needs just before the assignement with setcenter or center: and it just does not work. When I do the same with the geocoder then it works – Elias Echeverri Nov 29 '12 at 16:52
  • Please take a look at the new code I just put which uses the geocoder and that works perfecty.However I will get charged a lot if I use that implementation :-). Any ideas are welcome – Elias Echeverri Nov 29 '12 at 17:06
0

I just found a post from Google where they changed the LatLng function and now you have to CAST the two parameters with NUMBER(). They claim that people were passing strings to the LatLng function and that created unpredicatable results. So I have appended my new code with the changes ... It only took three days to find this fix!!!! :-( I wonder why more people are not running into this. Here is the code:

  <script type="text/javascript">
      (function () {

          // Defining global variables
          var map, geocoder, marker, infowindow, propertyaddress, selectedbuilding, maplatitude, maplongitude, buildinglatlng, latlng, myOptions;

          function InitializeMap() {
              //propertyaddress = '400 Alton Road, Miami Beach, FL 33139';
              propertyaddress = document.getElementById('<%=lblselectedHiddenBuildingAddress.ClientID%>').innerText;
              selectedbuilding = document.getElementById('<%=lblMainBuilding.ClientID%>').innerText;

              //maplatitude = parseFloat(document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText);
              //maplongitude = parseFloat(document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText);
              maplatitude = document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText;
              maplongitude = document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText;
              buildinglatlng = new google.maps.LatLng(Number(maplatitude), Number(maplongitude));

              //window.alert("Processed propertyaddress");

                      myOptions = {
                          zoom: 15,
                          center: new google.maps.LatLng(Number(maplatitude), Number(maplongitude)),
                          mapTypeId: google.maps.MapTypeId.ROADMAP,
                          scaleControl: true,
                          streetViewControl: true,
                          disableDefaultUI: true,
                          mapTypeControl: true,
                          mapTypeControlOptions: {
                              style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                              position: google.maps.ControlPosition.TOP_LEFT,
                              mapTypeIds: [
                                google.maps.MapTypeId.ROADMAP,
                                google.maps.MapTypeId.TERRAIN,
                                google.maps.MapTypeId.SATELLITE
                                ]
                          },
                          navigationControl: true,
                          navigationControlOptions: {
                              position: google.maps.ControlPosition.TOP_LEFT
                          }
                      };
                      // Creating the map
                      map = new google.maps.Map(document.getElementById("map"), myOptions); 

                      // Check to see if we've already got a Marker object
                      if (!marker) {
                          // Creating a new marker and adding it to the map
                          marker = new google.maps.Marker({
                              map: map,
                              animation: google.maps.Animation.DROP
                          });
                          google.maps.event.addListener(marker, 'click', toggleBounce);
                      }
                      // Setting the position of the marker to the returned location

                      marker.setPosition(buildinglatlng);

                      // Check to see if we've already got an InfoWindow object

                      google.maps.event.addListener(marker, 'click', function () {
                          if (!infowindow) {
                              // Creating a new InfoWindow
                              infowindow = new google.maps.InfoWindow();
                          }
                          // Creating the content of the InfoWindow to the address
                          // and the returned position
                          var content = '<h2>' + selectedbuilding + '</h2>';

                          // Adding the content to the InfoWindow
                          infowindow.setContent(content);
                          // Opening the InfoWindow
                          infowindow.open(map, marker);
                      });

                      // Triggering the click event
                      google.maps.event.trigger(marker, 'click');

          }

          function toggleBounce() {
              if (marker.getAnimation() != null) {
                  marker.setAnimation(null);
              } else {
                  marker.setAnimation(google.maps.Animation.BOUNCE);
              }
          }

          window.onload = InitializeMap;
      })();
</script>