谷歌地理编码器问题

发布于 2024-10-31 03:17:37 字数 4693 浏览 4 评论 0原文

我正在尝试对用户在文本字段中给出的地址进行地理编码,但是我的 JavaScript 函数不会从“geocoder.geocode ({'address':address......” ) 行开始执行。

我被困在这里不知道为什么它不起作用。它会抛出第一个警告框,显示地址字段已移至客户端脚本,但之后就不起作用了,请让我知道我需要在哪里进行更正。是客户端代码。

var map;
var geocoder;
function initialize()
{
    geocoder = new google.maps.Geocoder();  
}
function getgeocode() {  
    var address = document.advpollsrch.txtaddress.value;
    alert(address);
    geocoder.geocode( {'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            alert("Geocode was successful");
            var latlng = results[0].geometry.location;
            document.advpollsrch.hidlatitude.value = latlng.lat();
            document.advpollsrch.hidlongitude.value = latlng.lng();
            alert("Geocode split was successful");
            return true;
        } else {
            alert("Geocode was not successful for the following reason: " + status);
            return false;
        }
    });
}  
<body onload="initialize()">
    <form  name="advpollsrch"> <!-- onsubmit="return getgeocode()" -->
        <input type="hidden" name="hidlatitude" id="hidlatitude"></input>
        <input type="hidden" name="hidlongitude" id="hidlongitude"></input>

        <p style="font-weight:bold; float:left; margin-left:0px;">STEP 1 - </p>
        <div style="border: 1px dotted #000000;width: auto; height: auto; padding: 0px 8px 0px 8px; margin:0px 8px 0px 12px; float:left;">
            <table border="0" cellpadding="0" cellspacing="10">
                <tr>
                    <td>
                        <label for="staddress">Street Address</label><br />
                        <input type="text" width="300" name="txtaddress" id="txtaddress" value=""></input>
                    </td>
                    <td>
                        <label for="city">City</label><br />
                        <input type="text" width="150" name="txtcity" id="txtcity" value=""></input>
                    </td>
                </tr>
            </table>
        </div>
        <h3 style="padding: 0px 8px 0px 8px;float:left;">Or</h3>
        <div style="border: 1px dotted #000000; width: auto; height: auto;padding: 5px 8px 5px 8px; margin: 0px 8px 0px 12px; float:left;">
            <table border="0" cellspacing="5" cellpadding="0">
                <tr>
                    <td>
                        <label for="staddress">Town / RM/ Village / First Nation</label><br />
                        <input type="text" width="300" name="txtplace" id="txtplace" value=""></input>
                    </td>
                </tr>
            </table>    
        </div>
        <br /><br /><br /><br />
        <p style="font-weight:bold; float:left; margin-left:0px;">STEP 2 - </p> 
        <div style="border: 1px dotted #000000; width: auto; height: auto;padding: 0px 8px 2px 8px; margin: 0px 8px 0px 12px; float:left;"> 
            <table border="0" cellspacing="5" cellpadding="0">
                <tr>
                    <td>    
                        <label for="radius">Select the distance</label><br />
                        <select id="radius" name="radius">
                            <option value="0">Choose Distance:</option>
                            <option value="2">2 kilometers</option>
                            <option value="4">4 kilometers</option>
                            <option value="6">6 kilometers</option>
                            <option value="8">8 kilometers</option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>  
        <br /><br /><br />
        <p style="font-weight:bold; float:left;">STEP 3 - </p>
        <div style="padding: 15px 8px 2px 0px; margin: 0px 8px 0px 12px; float:left;">
            <input type="button" value="Find Advance Poll locations" onclick="getgeocode()"></input>    
        </div>
    </form>  <br />

    <div style="margin-top:50px; margin-left:-3px;">
        <iframe id="gmaps" name="gmaps" src="showpoints_jq.html" frameborder="no" scrolling="no" width="780px" height="520px"></iframe>
    </div>
</body>

I am trying to geocode an address given by the user in the text field, however my JavaScript function does not execute from the line "geocoder.geocode ({'address':address......" onwards.

I am stuck here not sure why it doesn't work. It throws thee first alert box showing the address field pulled over to the client script but does not work after that. It just stays without any action. Please let me know where i need to correct. Here is the client side code.

var map;
var geocoder;
function initialize()
{
    geocoder = new google.maps.Geocoder();  
}
function getgeocode() {  
    var address = document.advpollsrch.txtaddress.value;
    alert(address);
    geocoder.geocode( {'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            alert("Geocode was successful");
            var latlng = results[0].geometry.location;
            document.advpollsrch.hidlatitude.value = latlng.lat();
            document.advpollsrch.hidlongitude.value = latlng.lng();
            alert("Geocode split was successful");
            return true;
        } else {
            alert("Geocode was not successful for the following reason: " + status);
            return false;
        }
    });
}  
<body onload="initialize()">
    <form  name="advpollsrch"> <!-- onsubmit="return getgeocode()" -->
        <input type="hidden" name="hidlatitude" id="hidlatitude"></input>
        <input type="hidden" name="hidlongitude" id="hidlongitude"></input>

        <p style="font-weight:bold; float:left; margin-left:0px;">STEP 1 - </p>
        <div style="border: 1px dotted #000000;width: auto; height: auto; padding: 0px 8px 0px 8px; margin:0px 8px 0px 12px; float:left;">
            <table border="0" cellpadding="0" cellspacing="10">
                <tr>
                    <td>
                        <label for="staddress">Street Address</label><br />
                        <input type="text" width="300" name="txtaddress" id="txtaddress" value=""></input>
                    </td>
                    <td>
                        <label for="city">City</label><br />
                        <input type="text" width="150" name="txtcity" id="txtcity" value=""></input>
                    </td>
                </tr>
            </table>
        </div>
        <h3 style="padding: 0px 8px 0px 8px;float:left;">Or</h3>
        <div style="border: 1px dotted #000000; width: auto; height: auto;padding: 5px 8px 5px 8px; margin: 0px 8px 0px 12px; float:left;">
            <table border="0" cellspacing="5" cellpadding="0">
                <tr>
                    <td>
                        <label for="staddress">Town / RM/ Village / First Nation</label><br />
                        <input type="text" width="300" name="txtplace" id="txtplace" value=""></input>
                    </td>
                </tr>
            </table>    
        </div>
        <br /><br /><br /><br />
        <p style="font-weight:bold; float:left; margin-left:0px;">STEP 2 - </p> 
        <div style="border: 1px dotted #000000; width: auto; height: auto;padding: 0px 8px 2px 8px; margin: 0px 8px 0px 12px; float:left;"> 
            <table border="0" cellspacing="5" cellpadding="0">
                <tr>
                    <td>    
                        <label for="radius">Select the distance</label><br />
                        <select id="radius" name="radius">
                            <option value="0">Choose Distance:</option>
                            <option value="2">2 kilometers</option>
                            <option value="4">4 kilometers</option>
                            <option value="6">6 kilometers</option>
                            <option value="8">8 kilometers</option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>  
        <br /><br /><br />
        <p style="font-weight:bold; float:left;">STEP 3 - </p>
        <div style="padding: 15px 8px 2px 0px; margin: 0px 8px 0px 12px; float:left;">
            <input type="button" value="Find Advance Poll locations" onclick="getgeocode()"></input>    
        </div>
    </form>  <br />

    <div style="margin-top:50px; margin-left:-3px;">
        <iframe id="gmaps" name="gmaps" src="showpoints_jq.html" frameborder="no" scrolling="no" width="780px" height="520px"></iframe>
    </div>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文