这是被调用的两个函数,一个来自HTML代码,另一个来自PHP代码。函数addMarker
用于在从数据库检索到的位置添加标记。函数codeAddress
用于在用户以HTML形式输入的位置处放置标记。codeAdress
工作正常,但"addMarker"不正常。我还尝试过在addMarker函数中执行警报消息,它运行良好。然而,地理编码器功能在其内部不起作用。我还在实际代码下面为两个函数编写函数调用。
function addMarker(address1)
{
geocoder.geocode( {'address': address1}, function(results, status) {
alert(address1);
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
draggable: true,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function codeAddress() {
var address = document.getElementById("address").value;
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,
draggable: true,
position: results[0].geometry.location
});
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
html代码
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="codeAddress()">
PHP代码
echo "<script type='text/javascript'>";
echo "addMarker('$row[address]')";
echo "</script>";
$row[address]在这里没有被求值,它只是被回显。我怀疑你的意思是:
echo "addMarker(" . $row['address'] . ")";
但我尝试使用alert在JavaScript代码中打印address1变量。而且印刷得很完美。我不认为这是错误。