我正在尝试将相同的代码用于两个不同的函数.然而,一个功能正在工作,而另一个则不工作


I am trying to use the same code for two different functions.. However one function is working but the other one is not

这是被调用的两个函数,一个来自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变量。而且印刷得很完美。我不认为这是错误。