Reputation: 99
I have a problem with my script. I have everything set correctly but the geocoding does not work, when I enter the name of a city in the form that I created then I click on "geocoder" it nothing happens. This is my code :
Variable :
var geocoder;
var map;
var markers = new Array();
var i = 0;
Geocoding Script :
<!-- GEOCODER -->
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
markers.push(marker);
if(markers.length > 1)
markers[(i-1)].setMap(null);
i++;
} else {
alert("Le geocodage n\'a pu etre effectue pour la raison suivante: " + status);
}
<!-- GEOCODER -->
Form:
<div>
Adresse : <input id="address" type="text" value="paris">
<input type="button" value="Geocoder" onsubmit="codeAddress()">
</div>
Full code ( Only html) : http://jsfiddle.net/hCmQb/
Upvotes: 0
Views: 1301
Reputation: 161404
The reason the geocoder is not running is this:
<input type="button" value="Geocoder" onsubmit="codeAddress()">
You don't want "onsubmit" you want "onclick":
<input type="button" value="Geocoder" onclick="codeAddress()">
The other problem you have is that your "map" variable is local to your initialize function. Change:
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
to:
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
Upvotes: 2