Reputation: 1
I'm trying to geocode an address from Microsoft Dynamics CRM 2011 using Javascript. I don't want to display the map, just perform the Geocoding and store the Lat / Long values in the database. I keep getting an error message saying
"result.0 is null or not an object"
var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=39 George Street, Belmont, WA, 6104, Australia&sensor=false'
var lat = url.results[0].geometry.location.lat();
alert(lat)
what am I doing wrong?
Upvotes: 0
Views: 1058
Reputation: 968
I have to disagree with Greg...you appear to have the request URL pretty close. Check out the Google Geocoding guide for the exacts. What you will get back is a JSON string, so you should use something like jQuery to parse the string into an object.
If you are not set on using JSON, then a simple script like this will also return the Lat/Lng (adapted from Google sample):
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var addressLocation = results[0].geometry.location;
alert("Lat: " + addressLocation.lat() + " Lon: " + addressLocation.lng());
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
Upvotes: 1
Reputation: 3878
I assume from your question that this is a complete code snippet. On that assumption your code is incomplete. Your code creates a string called url
. You then attempt to access a collection in that string, called results
(which does not exist - and therefore position 0 within that colelction does not exist). Basically, your code doesn't do anything.
You need to have a good read of the Google Geocode API because, I'm sorry to say, you're not even close to getting this working... https://developers.google.com/maps/documentation/javascript/geocoding
Edit: In the interests of being more helpful - here is a walkthrough too: http://www.wikihow.com/Geocode-an-Address-in-Google-Maps-Javascript
Upvotes: 2