我对JavaScript非常陌生,目前正在尝试使用它,并练习使用Google-geocoder函数来打印文本字段中的经度和纬度。我确信我的代码包含错误等等。不管怎样,我的JS代码(GeoCoder.js)看起来像这样:
function codeAddress()
{
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value;
geocoder.geocode( {'address': address}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK)
{
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
}我还构造了一个超文本标记语言文件(Addresses.html),我希望从该文件中读取地址并能够输出经度/纬度。我的html文件如下所示:
<head>
<script type="text/javascript" src="GeoCoder.js"></script>
</head>
<h2>Location</h2>
<form name="myform">
<div>
Address:<br>
<input type="text" id="address" name="address">
<input type="button" name="Click" id="firstButton" onclick="codeAddress()">
</div>
<div>
Output:<br>
<input type="text" id="longitude">
<input type="text" id="latitude">
</div>
</form>我意识到到目前为止我所拥有的是不完整的,并且不能像它所期望的那样工作。我需要帮助指出任何错误,并完成任务,以便当我在文本字段中输入地址并按下它旁边的按钮时,经度和纬度会在地址下面的输出字段中输出。
发布于 2016-05-19 01:47:41
你能不能试一下,告诉我结果是什么,这样我就可以帮你了……
function codeAddress()
{
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value;
alert("address : "+address);// should display text you entered in input field
geocoder.geocode( {'address': address}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK)
{
alert("entered if statement");// you have entered if statement
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert("latitude : "+latitude+" : longitude : "+longitude);// display values of latitude and longitude.
document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
}如果你得到了所有的警告信息,除了最后一个正确的值,仍然不能在输入字段中设置值,试试这个…
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude; https://stackoverflow.com/questions/37304295
复制相似问题