I’m making a page that utilizes the Google Maps geocoder object to find a user entered address. I am able to find the lat/lng points, but I’d like to format the address (clean it up) so I can store the Lat, Lng and Address in 3 separate fields in a MySQL db. Here is my current code:
also feel free to criticize the current code in the scope of security, I’m completely new to the Gmaps API and not very skilled with js
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Map</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=true_or_false&key=ABQIAAAAKgJa4e_5XNFRJJF7MsI1eRQN1RsnicNCcG4bidjiZE76b9vSTBTCbiFNb-LGPKHeiBuV_2yfnWNSTA" type="text/javascript">
</script>
</head>
<body>
Input address Here: <input type="text" name="locate_address" id="locate_address" />
<input type="button" name="locate_address" value="Locate Address" onclick="locateAddress()" />
<div>
<p id="lat">Lat: </p>
<p id="lng">Lng: </p>
<p id="address"></p>
</div>
<br />
<div id="map" style="width: 300px; height:300px"></div>
<script type="text/javascript">
function locateAddress() {
var address = document.getElementById('locate_address').value;
geocoder = new GClientGeocoder();
geocoder.getLatLng (
address,
function (point) {
if (!point) {
alert(address + " not found!");
}
else {
map.setCenter(point, 13);
var marker = new GMarker (point);
map.addOverlay (marker);
marker.openInfoWindowHtml(address);
document.getElementById('address').innerHTML = address;
document.getElementById('lat').innerHTML += point.y;
document.getElementById('lng').innerHTML += point.x;
}
})
}
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(39.1700149, -86.5148133), 13);
map.setMapType(G_NORMAL_MAP);
</script>
</body>
</html>
If you really want to use v2 API
for v3 see
http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingResponses