We help IT Professionals succeed at work.
Get Started

JAVAScript Function for Google Maps API

Last Modified: 2013-11-10
I am attempting to update form fields with lat/long and address information based on the position of a draggable marker; however, I cannot get the address information to update when the marker is moved.  It seems that the getAddress function does not execute when the marker is released.

The JAVAScript is included with this post or you can easily see the entire page with form fields in action at http://www.dwycowarehousesales.com/test.asp.  In the uploaded version, the GEvent.addListener is uncommented, which will cause the address field to be updated based on the location you click on the map.  The lat/long information updates as expected, but the address information does not update with marker release.

I'm sure you can tell that I'm not familiar with JAVAScript, so very much appreciate your help with this!
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAjqvq9ft21y-9k7Gvk3kA9xTw6H2RdkhLyjN942TxVhjhl8xtORQ8VNSa8bRlowcU2DOdQvNH1DKHng"
<script type="text/javascript">
  var map;
  var geocoder;
  var address;
  function initialize() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map_canvas"));
      var center = new GLatLng(40.730885,-73.997383);
      map.setCenter(center, 15);
      map.addControl(new GLargeMapControl);
      map.addControl(new GMapTypeControl());
      var marker = new GMarker(center, {draggable: true});
      //GEvent.addListener(map, "click", getAddress);
      GEvent.addListener(marker, "dragend", function() {
        var position = marker.getPoint();
        map.panTo(new GLatLng(position.lat(), position.lng()));
        coordinates.latlonpos.value = position.lat()+", "+position.lng();
         getAddress();//THIS DOES NOT EXECUTE
      geocoder = new GClientGeocoder();
  function getAddress(overlay, latlng) {
    if (latlng != null) {
      address = latlng;
      geocoder.getLocations(latlng, showAddress);
  function showAddress(response) {
    if (!response || response.Status.code != 200) {
      alert("Status Code:" + response.Status.code);
      coordinates.streetaddress.value = response.Status.code
    } else {
      place = response.Placemark[0];
      coordinates.streetaddress.value = place.address

Open in new window

Watch Question
Analyst developer
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE