【发布时间】: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