I’m using Googles Map API V3 to geocode an address, then I want to store the Latitude and Longitude that it is returning. The Latitude that I am hard coding to “35” is showing up as expected, however I’m failing to capture the “results” of the GeoCode request. Can anyone spot where I am missing them at?
function loadScript()
{
var script = document.createElement("script");
script.src = "http://maps.googleapis.com/maps/api/js? key=AIzaSyDaukwjA3JdWZhHyUhvxiEHfKAjPhAz8jM&sensor=false&callback=initialize"; document.body.appendChild(script);
}
window.onload = loadScript;
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 17,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
function codeAddress() {
var addresstxt = document.getElementById("properties_street_address").value;
var citytxt = document.getElementById("properties_city").value;
var statetxt = document.getElementById("properties_state").value;
var zipcodetxt = document.getElementById("properties_zip_code").value;
var address = addresstxt + ' ' + citytxt+','+ statetxt+' '+zipcodetxt;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var lat = '35';
var lng = results[0].geometry.location.lng;
document.getElementById("properties_lattitude").value = lat;
document.getElementById("properties_longitude").value = lng;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}