Need Javascript to pass address field to a function (Google Maps API)

I'm working on an implementation of the Google Maps API and am running into a bit of a problem.  Currently, I've got a pop-up box next to a set of latitude/longitude boxes.  The pop-up allows the user to punch in their address and hit submit, and via Javascript, the Lat/Long are displayed.  The user then copies and pastes the information back into the parent form which gets saved in a database for future map generation.

The problem is that this solution is evidently incompatible with Internet Explorer, but more importantly it's a big waste of time.  What I'd like to do is keep the pop-up link where it is, but rather than have it pop up another window, have it query the address field above and then run the routine to collect the latitude/longitude and populate the field.

Here's what I've got now on the main form:

<label for="formAddress1">Address:</label>
<input type="text" name="formAddress1" id="formAddress1" value="">
<br class="clear">
<label for="formAddress2">Address (line 2):</label>
<input type="text" name="formAddress2" id="formAddress2" value="">
<br class="clear">
<label for="formCity">City:</label>
<input type="text" name="formCity" id="formCity" value="">
<br class="clear">
<label for="formState">State:</label>
<select name="formState" id="formState">
<option value="">Please Select</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
...etc...
</select>
<br class="clear">
<label for="formZip">Zip:</label>
<input type="text" name="formZip" id="formZip" value="">
<br class="clear">            
<label for="formLat">Latitude:</label>
<input type="text" name="formLat" id="formLat" value="">
<input type="text" name="formLong" id="formLong" value="">
<a href="javascript:popUp('/account_faq.asp#coordinates');"><img src="/images/form_question.gif" class="question" border="0"></a>
<br class="clear">

That opens up to my popup with the Google Maps API:
<form action="#" onsubmit="showAddress(this.address.value); return false">
            <input type="text" size="60" name="address" value="" />
            <input type="image" src="/images/btn_find_coordinates.gif" class="submit_account">
      </form>
      <table border="0" cellpadding="0" cellspacing="0" width="228">
            <tr>
                  <td width="100"><b>Latitude</b></td>
                  <td id="lat"></td>
            </tr>
            <tr>
                  <td width="100"><b>Longitude</b></td>
                  <td id="lng"></td>
            </tr>
      </table>
      <div id="map">
            <br/>
      </div>

Which is handled by this included file:
function load(lat,lng) {
  if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      var center = new GLatLng(lat, lng);
      map.setCenter(center, 9);
      geocoder = new GClientGeocoder();
      var marker = new GMarker(center, {draggable: true});  
      map.addOverlay(marker);
      document.getElementById("lat").innerHTML = center.lat().toFixed(5);
      document.getElementById("lng").innerHTML = center.lng().toFixed(5);

  GEvent.addListener(marker, "dragend", function() {
   var point = marker.getPoint();
        map.panTo(point);
   document.getElementById("lat").innerHTML = point.lat().toFixed(5);
   document.getElementById("lng").innerHTML = point.lng().toFixed(5);

      });


 GEvent.addListener(map, "moveend", function() {
        map.clearOverlays();
var center = map.getCenter();
        var marker = new GMarker(center, {draggable: true});
        map.addOverlay(marker);
        document.getElementById("lat").innerHTML = center.lat().toFixed(5);
   document.getElementById("lng").innerHTML = center.lng().toFixed(5);


 GEvent.addListener(marker, "dragend", function() {
  var point =marker.getPoint();
       map.panTo(point);
  document.getElementById("lat").innerHTML = point.lat().toFixed(5);
       document.getElementById("lng").innerHTML = point.lng().toFixed(5);

      });

      });

  }
}

   function showAddress(address) {
   var map = new GMap2(document.getElementById("map"));
   map.addControl(new GSmallMapControl());
   map.addControl(new GMapTypeControl());
   if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
        document.getElementById("formLat").innerHTML = point.lat().toFixed(5);
   document.getElementById("formLng").innerHTML = point.lng().toFixed(5);
       map.clearOverlays()
            map.setCenter(point, 14);
var marker = new GMarker(point, {draggable: true});  
       map.addOverlay(marker);

      GEvent.addListener(marker, "dragend", function() {
  var pt = marker.getPoint();
       map.panTo(pt);
  document.getElementById("formLat").innerHTML = pt.lat().toFixed(5);
       document.getElementById("formLng").innerHTML = pt.lng().toFixed(5);
      });


 GEvent.addListener(map, "moveend", function() {
        map.clearOverlays();
var center = map.getCenter();
        var marker = new GMarker(center, {draggable: true});
        map.addOverlay(marker);
        document.getElementById("lat").innerHTML = center.lat().toFixed(5);
   document.getElementById("lng").innerHTML = center.lng().toFixed(5);

 GEvent.addListener(marker, "dragend", function() {
 var pt = marker.getPoint();
      map.panTo(pt);
document.getElementById("lat").innerHTML = pt.lat().toFixed(5);
   document.getElementById("lng").innerHTML = pt.lng().toFixed(5);
      });

      });

            }
        }
      );
  }
}


So, what I'd like to do is keep the link where it is but instead of popping up another window, have it populate the formLat and formLong fields with the result of showAddress, I believe, which in turn is getting its data from formAddress1, formCity, formState, and formZip.  Anyone able to point me in the right direction?  Please keep in mind that I'm only slightly above beginner level when talking about JavaScript.

Thanks very much in advance!
LVL 8
saoirse1916Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tony McCreathTechnical SEO ConsultantCommented:
it sounds a bit like something I implemented on my website:

http://www.mccreath.org.uk/Article/Google-Maps-Location-Finder_42.aspx

The javascript for this is in the http://www.mccreath.org.uk/Shared/GoogleMaps.js file.

I've included a simpilfication of the code that reads an address field, finds the lat/lng and populates the relevant fields.

It assumes input fields with ids "address", "lat" & "lng". And function moveEditorMarkerAddress() needs to be called to update the lat/lng fields

var ClientGeocoder = new GClientGeocoder();
function moveEditorMarkerAddress() { 
    ClientGeocoder.getLocations(document.getElementById("address").value,  moveEditorMarkerAddressResponse);
}
 
function moveEditorMarkerAddressResponse(response) {
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address: "+ response.Status);
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
 
 
        document.getElementById("lat").value = point.lat(); 
        document.getElementById("lng").value = point.lng();
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saoirse1916Author Commented:
That works great!  Is there a way to add in the city, state, and zip to the lookup?  Something like this (again, forgive the incorrect JavaScript syntax:

ClientGeocoder.getLocations(document.getElementById("address").value + ", " + document.getElementByID("city").value + ", " + document.getElementByID("state").value + " " + document.getElementByID("zip").value,  moveEditorMarkerAddressResponse);

When I try that, I get an error so I'm assuming I've screwed it up somehow.
0
Tony McCreathTechnical SEO ConsultantCommented:
javascript is case sensitive so make sure your getElementByID = getElementById.

Do a check to see if your getting the field data correct first...

alert(document.getElementById("address").value + ", " + document.getElementById("city").value + ", " + document.getElementById("state").value + " " + document.getElementById("zip").value);


0
saoirse1916Author Commented:
Bingo, that must have been it.  Thanks very much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.