0

The address I have is a concatenated results[1].formatted_address retrieved from a database containing 6 numbers e.g 530456. I then geocoded it and placed a marker on a google map. This part is successful.

I now want to display the full address, results[0].formatted_address in a infowindow of the marker, so what I did was to reverse geocode the latLng of the marker to obtain the full address but caused the entire map to disappear. The way I did it is as such:

var address;
var eventwindow;
function codeAddress(postal) {
    geocoder.geocode( {'address': postal + ", Singapore"}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        eventwindow = new google.maps.InfoWindow();
        var markerE = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location,
        });

        address = getMarkerAddress(results[0].geometry.location);

        google.maps.event.addListener(marker, 'click', function(){
            if(eventwindow)
                eventwindow.close();
            eventwindow = new google.maps.InfoWindow({
                content: address,
            });
            eventwindow.open(map,marker);
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

function getMarkerAddress(location) {
    geocoder.geocode({'latLng': location}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
          if(results[0])
          {
            address = results[1].formatted_address.substring(10);
          }
        }
       else {
        alert("Geocoder failed due to: " + status);
      }
    });

I don't know if I'm doing this right and and not sure where I went wrong. Is there another way to this problem? If so how?

consprice
  • 722
  • 3
  • 11
  • 21

1 Answers1

0

I don't know if I'm doing this right and and not sure where I went wrong. Is there another way to this problem? If so how?

If you know the "correct" address, use that. You haven't provided enough details of your application to make suggestions on how to do that. You can avoid the uncertainty of the geocoder by storing the coordinates that are returned from the geocoder in your database along with the address, then if you find an error, you can fix it.

This article on geocoding strategies might help.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • I don't know the "correct" address. The current application allows users to create events with details such as location, by dropping a pin on the map, which returns the 6 digit number postal code, and is stored into database. Upon viewing the event, the location of event is seen on the map as pin with details on displayed on marker's infowindow. I want to avoid modifying the database as that would mean modifying entity classes and java servlets as well. – consprice Jul 11 '12 at 02:43
  • Your article gave me what I needed to know. Looks like I should be doing server-side geocoding instead. Thanks for the help:) – consprice Jul 11 '12 at 02:50