从表格读取地址并输出经度&纬度 [英] Reading address from form and outputting longitude & latitude

查看:150
本文介绍了从表格读取地址并输出经度&纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript很陌生,目前正在玩弄它,并尝试使用Google地理编码器函数打印文本字段中的经度和纬度。无论如何,我所拥有的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 =纬度;
document.getElementById(longitude)。value = longitude;
}
else
{
alert(Geocode不成功,原因如下: +状态);
}
});
}

我也构建了一个HTML文件(Addresses.html)期望读取地址并能够输出经度/纬度。我的html文件如下所示:

 < head> 
< script type =text / javascriptsrc =GeoCoder.js>< / script>
< / head>
< h2>位置< / h2>
< form name =myform>
< div>
地址:< br>
< input type =textid =addressname =address>
< input type =buttonname =Clickid =firstButtononclick =codeAddress()>
< / div>
< div>
输出:< br>
< input type =textid =longitude>
< input type =textid =latitude>
< / div>
< / form>

我知道我到目前为止所做的并不完整,至。我希望能帮助指出任何错误并完成任务,以便当我在文本字段中输入地址并按下旁边的按钮时,经度和纬度将在地址下方的输出字段中输出。 / p>

解决方案

你可以试试这个,告诉我结果是什么,这样我就可以帮你.... b
$ b

 函数codeAddress()
{
var geocoder = new google.maps.Geocoder();
var address = document.getElementById(address)。value;
alert(address:+ address); //应显示您在输入栏中输入的文本
geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK)
{
alert(entered if statement); //如果语句
var latitude = results [0] .geometry.location.lat();
var longitude = results [0] .geometry.location.lng();
alert(latitude:+ latitude +:longitude:+ longitude); //显示经度和纬度的值
document.getElementById(latitude)。value = latitude;
document.getElementById(longitude)。value = longitude;
}

{
alert(由于以下原因,Geocode不成功:+ status;
}
});

$ / code>

如果您收到所有警告讯息,能够设置输入字段中的值试试这... ...

pre $ document.getElementById(latitude)。innerHTML = latitude;
document.getElementById(longitude)。innerHTML = longitude;


I'm quite new to JavaScript and am currently playing around with it and practicing trying to get a Google-geocoder function to print out the longitude and latitude in text fields. I'm sure the code I have contains errors etc. Anyhow, the JS code I have (GeoCoder.js) looks like this:

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);
        }
    }); 
}

I have also constructed a HTML file (Addresses.html), from which I expect to read an address and be able to output the longitude/latitude. My html file looks as follows:

<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>

I'm aware that what I have so far is incomplete, and does not function as it is supposed to. I'd like help with pointing out any errors, and in completing the task so that when I enter an address in the text field and push the button next to it, the longitude and latitude gets outputted in the output fields below the address.

解决方案

can you try this and tell me what is the result so that i can help you....

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);
}
}); 
}

if you get all alert message except the last one with correct values and still not able to set the values in input field try this...

document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;    

这篇关于从表格读取地址并输出经度&amp;纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆