0

我使用jQuery在asp.net中获取地理坐标下面是代码

我想在文本框中输入地址并在标签中显示纬度和经度。有人可以建议我正确的方式

<script type="text/javascript">
    var geocoder = new google.maps.Geocoder();
    var address = "adeliade";

    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
            alert(latitude + ", " + longitude);
        }
    });
</script>
4

1 回答 1

0

检查这是否有效。

放置以下HTML:

Address: <input type="text" id="tbCity" />
<input type="button" id="btn" value="Show Lat/Long" /> <!-- note that, this is not an asp server button control; just an html button. It should not cause a postback  --!>

head以及标签中的这些 javascripts(jQuery) :

<script type="text/javascript">
    $(function(){
        geocoder = new google.maps.Geocoder();

        $("#btn").on("click", function () {
            var address = $("#tbCity").val();
            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();
                    alert(latitude + ", " + longitude);
                }
            });
        });
    });
</script>
于 2013-05-28T02:43:41.967 回答