我正在尝试对用户在文本字段中给出的地址进行地理编码,但是我的JavaScript函数不会从“geocoder.geocode({'address':address ......”)行开始执行。< / p>
我被困在这里不确定为什么它不起作用。它会抛出第一个警告框,显示地址字段被拉到客户端脚本但在此之后不起作用。它只是没有任何行动。请让我知道我需要纠正的地方。这是客户端代码。
var map;
var geocoder;
function initialize()
{
geocoder = new google.maps.Geocoder();
}
function getgeocode() {
var address = document.advpollsrch.txtaddress.value;
alert(address);
geocoder.geocode( {'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert("Geocode was successful");
var latlng = results[0].geometry.location;
document.advpollsrch.hidlatitude.value = latlng.lat();
document.advpollsrch.hidlongitude.value = latlng.lng();
alert("Geocode split was successful");
return true;
} else {
alert("Geocode was not successful for the following reason: " + status);
return false;
}
});
}
<body onload="initialize()">
<form name="advpollsrch"> <!-- onsubmit="return getgeocode()" -->
<input type="hidden" name="hidlatitude" id="hidlatitude"></input>
<input type="hidden" name="hidlongitude" id="hidlongitude"></input>
<p style="font-weight:bold; float:left; margin-left:0px;">STEP 1 - </p>
<div style="border: 1px dotted #000000;width: auto; height: auto; padding: 0px 8px 0px 8px; margin:0px 8px 0px 12px; float:left;">
<table border="0" cellpadding="0" cellspacing="10">
<tr>
<td>
<label for="staddress">Street Address</label><br />
<input type="text" width="300" name="txtaddress" id="txtaddress" value=""></input>
</td>
<td>
<label for="city">City</label><br />
<input type="text" width="150" name="txtcity" id="txtcity" value=""></input>
</td>
</tr>
</table>
</div>
<h3 style="padding: 0px 8px 0px 8px;float:left;">Or</h3>
<div style="border: 1px dotted #000000; width: auto; height: auto;padding: 5px 8px 5px 8px; margin: 0px 8px 0px 12px; float:left;">
<table border="0" cellspacing="5" cellpadding="0">
<tr>
<td>
<label for="staddress">Town / RM/ Village / First Nation</label><br />
<input type="text" width="300" name="txtplace" id="txtplace" value=""></input>
</td>
</tr>
</table>
</div>
<br /><br /><br /><br />
<p style="font-weight:bold; float:left; margin-left:0px;">STEP 2 - </p>
<div style="border: 1px dotted #000000; width: auto; height: auto;padding: 0px 8px 2px 8px; margin: 0px 8px 0px 12px; float:left;">
<table border="0" cellspacing="5" cellpadding="0">
<tr>
<td>
<label for="radius">Select the distance</label><br />
<select id="radius" name="radius">
<option value="0">Choose Distance:</option>
<option value="2">2 kilometers</option>
<option value="4">4 kilometers</option>
<option value="6">6 kilometers</option>
<option value="8">8 kilometers</option>
</select>
</td>
</tr>
</table>
</div>
<br /><br /><br />
<p style="font-weight:bold; float:left;">STEP 3 - </p>
<div style="padding: 15px 8px 2px 0px; margin: 0px 8px 0px 12px; float:left;">
<input type="button" value="Find Advance Poll locations" onclick="getgeocode()"></input>
</div>
</form> <br />
<div style="margin-top:50px; margin-left:-3px;">
<iframe id="gmaps" name="gmaps" src="showpoints_jq.html" frameborder="no" scrolling="no" width="780px" height="520px"></iframe>
</div>
</body>