首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从表单中读取地址并输出经纬度

从表单中读取地址并输出经纬度
EN

Stack Overflow用户
提问于 2016-05-18 23:40:01
回答 1查看 93关注 0票数 0

我对JavaScript非常陌生,目前正在尝试使用它,并练习使用Google-geocoder函数来打印文本字段中的经度和纬度。我确信我的代码包含错误等等。不管怎样,我的JS代码(GeoCoder.js)看起来像这样:

代码语言:javascript
复制
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文件如下所示:

代码语言:javascript
复制
<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>

我意识到到目前为止我所拥有的是不完整的,并且不能像它所期望的那样工作。我需要帮助指出任何错误,并完成任务,以便当我在文本字段中输入地址并按下它旁边的按钮时,经度和纬度会在地址下面的输出字段中输出。

EN

回答 1

Stack Overflow用户

发布于 2016-05-19 01:47:41

你能不能试一下,告诉我结果是什么,这样我就可以帮你了……

代码语言:javascript
复制
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);
}
}); 
}

如果你得到了所有的警告信息,除了最后一个正确的值,仍然不能在输入字段中设置值,试试这个…

代码语言:javascript
复制
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37304295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档