Reputation: 201
Hi i am getting geocoder undefined error. What i trying is to show google map on page load. Below is my code
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
$(document).ready(function(){
var address = document.getElementById('shipAddress').value;
//alert(address);
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
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
});
google.maps.event.addDomListener(window, 'load', initialize);
Any help will be appreciated!!!
Upvotes: 2
Views: 5850
Reputation: 41
Also make sure you have
<script src="https://maps.google.com/maps?file=api&v=3&sensor=false"
type="text/javascript"></script>
prior to running the javascript code.
That's what messed me up.
Upvotes: 1
Reputation: 117324
document.ready
(where you try to access geocoder
) will fire before window.onload
(where you create the Geocoder
-instance).
Append the code for document.ready
to initialize
Upvotes: 2