Click to See Complete Forum and Search --> : Geocoding an Address on form submission


animai
06-20-2010, 10:11 PM
I have just spent a couple days batch geocoding and updating about 60k records in my clients database. I want to include an automatic feature on the form so when a person creates a new profile their address will be automatically geocoded if they do no provide coordinates.

I know I can pass an address to google's geocode service through a URL but I was looking for a more straightforward way as the method to retrieve the lat/long seemed long and tedious for what I would get in return-I also have limited experience with XML (can do it but prefer to avoid :o).

Does anyone know of an ASP function I could use in place? Any website that may have more information? I have tried looking but only get results for ASP.NET. I do not have direct access to the server but if I need to get something loaded into it for this to work I can have it done. Any tips or direction would be greatly appreciated! :D

yamaharuss
06-24-2010, 01:40 PM
I wrote a very cool script to do just what you want.. let me find it.

yamaharuss
06-24-2010, 01:51 PM
This is all the code you need to test this. Just place this in an ASP file and call it in your browser. I left the map popup in the script but you can place in a hidden div.

YOU WILL NEED TO add your Google map key to the first line for this to work.

How it works:

Type in an address, city, state and zip in the form fields, once all field are filled a "get it" link will appear next to the latitude form field. Click that and the Lat/Long will fill in and a map will appear.



<script src="http://maps.google.com/maps?file=api&v=2&key=YOURKEYHERE" type="text/javascript"></script>

<script type="text/javascript">

var geocoder;
var map;

var restaurant = "The Old Mohawk Restaurant";
//var address = "821 Mohawk Street, Columbus OH";

// On page load, call this function

function loadGeo(address)
{
document.getElementById("fillGeo").innerHTML="<a href=\"#\" onClick=\"loadGeo('"+address+"');return false\">try again<\/a>";
// Create new map object
map = new GMap2(document.getElementById("map"));

// Create new geocoding object
geocoder = new GClientGeocoder();

// Retrieve location information, pass it to addToMap()
geocoder.getLocations(address, addToMap);
}

// This function adds the point to the map

function addToMap(response)
{
// Retrieve the object
place = response.Placemark[0];

// Retrieve the latitude and longitude
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
thisLat = place.Point.coordinates[1];
thisLon = place.Point.coordinates[0]
document.getElementById("txtLat").value=thisLat;
document.getElementById("txtLon").value=thisLon;
document.getElementById("filledGeo").innerHTML="<a href=\"#\" onClick=\"load(thisLat,thisLon);return false\">test map</a>";


// Center the map on this point
map.setCenter(point, 13);

// Create a marker
marker = new GMarker(point);

// Add the marker to map
map.addOverlay(marker);

// Add address information to marker
marker.openInfoWindowHtml(place.address);

}


function fillGeo() {
txtStreet = document.getElementById("txtStreet").value;
txtCity = document.getElementById("txtCity").value;
txtState = document.getElementById("txtState").value;
txtPostalCode = document.getElementById("txtPostalCode").value;
//loadGeo(""+txtStreet+"+"+txtCity+"+"+txtState+"+"+txtPostalCode+"");
if (txtStreet != "" && txtCity != "" && txtState != "" && txtPostalCode != ""){
document.getElementById("fillGeo").innerHTML="<a href=\"#\" onClick=\"loadGeo('"+txtStreet+"+"+txtCity+"+"+txtState+"+"+txtPostalCode+"');return false\">get it<\/a>";
}
else {
document.getElementById("fillGeo").innerHTML="";
}

}

</script>
<div id="map" style="width: 400px; height: 300px"></div>
<div align="center">
<TABLE border=5 cellpadding="0" bgcolor="AAAAAA">
<form action="#" method=post>
<TR><TD align="Right" valign="Bottom"><font Size='-1'><B>Street:</B> </font></TD>
<TD align="Left" valign="Bottom"><INPUT NAME="txtStreet" id="txtStreet" TYPE="Text" Size="30" MaxLength="50" onblur="fillGeo()"></TD>
</TR>
<TR><TD align="Right" valign="Bottom"><font Size='-1'><B>City:</B> </font></TD>
<TD align="Left" valign="Bottom"><INPUT NAME="txtCity" id="txtCity" TYPE="Text" Size="15" Value="Tallahassee" MaxLength="50" onblur="fillGeo()"></TD>
</TR>
<TR><TD align="Right" valign="Bottom"><font Size='-1'><B>State:</B> </font></TD>
<TD align="Left" valign="Bottom"><INPUT NAME="txtState" id="txtState" TYPE="Text" Size="2" Value="FL" MaxLength="2" onblur="fillGeo()">&nbsp; &nbsp; <font Size='-1'><B>Zip:</B> <INPUT NAME="txtPostalCode" id="txtPostalCode" TYPE="Text" Size="6" MaxLength="15" onblur="fillGeo()"></font></TD>
</TR>
<TR><TD align="Right" valign="Bottom"><font Size='-1'><B>Latitude:</B> </font></TD>
<TD align="Left" valign="Bottom"><INPUT NAME="txtLat" id="txtLat" TYPE="Text" Size="15" MaxLength="50">&nbsp; &nbsp; &nbsp; <span id="fillGeo"></span></TD>
</TR>
<TR><TD align="Right" valign="Bottom"><font Size='-1'><B>Longitude:</B> </font></TD>
<TD align="Left" valign="Bottom"><INPUT NAME="txtLon" id="txtLon" TYPE="Text" Size="15" MaxLength="50">&nbsp; &nbsp; &nbsp; <span id="filledGeo"></span></TD>
</TR>
<TR><TD align="Right" valign="Bottom">&nbsp;</TD>
<TD align="Left" valign="Bottom">
</TD>
</TR>
</table>

Dani_315
07-03-2010, 07:57 AM
Can we use in UK if Yes... Please explain modification type

animai
07-22-2010, 02:04 AM
Thanks, I will try it out when I get back to this project (something managed to break in the interim...)

Dani_315
01-03-2011, 09:13 PM
Thanks ........ Geo ?