【问题标题】:I am trying to use the same code for two different functions.. However one function is working but the other one is not我正在尝试对两个不同的功能使用相同的代码。但是一个功能可以工作,而另一个不能
【发布时间】:2014-09-30 13:06:32
【问题描述】:

这是两个被调用的函数,一个来自 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>";   

【问题讨论】:

  • 控制台有错误吗?
  • 在您提供的代码中,您在哪里调用addMarker
  • 而且您很可能在加载地图之前调用 addMarker。
  • @JGRICE 在 PHP 代码中调用该函数。它位于实际代码的末尾。
  • @JayBlanchard 没有控制台中没有任何错误。

标签: javascript php google-maps


【解决方案1】:

$row[address] 在这里没有被评估,它只是被回显。我怀疑你的意思是:

echo "addMarker(" . $row['address'] . ")";

【讨论】:

    【解决方案2】:

    但我尝试使用警报在 JavaScript 代码中打印 address1 变量。它完美地打印出来了。我不认为这是错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-09
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多