为什么jQuery函数不返回任何值?

时间:2016-09-21 06:18:59

标签: javascript jquery

用于计算并从给定地址返回lat和long的jQuery函数不起作用。我从表单中获取地址输入,我可以在jQuery函数中显示地址,但地理编码不返回任何内容。

<?php
    echo $this->Form->input('address_street',['id' => 'street']);
    echo $this->Form->input('address_suburb',['id' => 'suburb']);
    echo $this->Form->input('address_postcode',['id' => 'postcode']);
    echo $this->Form->input('address_state',['id' => 'state','type' => 'select', 'options' => $auStates, 'default' => 'VIC']);
?>
<hr>
<?php
    echo $this->Form->input('address_lat',['id' => 'lat']);
    echo $this->Form->input('address_long',['id' => 'long']);
?>

$(document).ready(function($) {
    var geocoder = new google.maps.Geocoder();

    $(document).on('click', '#calculate_address_lat_long', function () {
        var address = '';
        address += $('#street').val();
        address += ' ' + $('#suburb').val();
        address += ' ' + $('#postcode').val();
        address += ' ' + $('#state').val();

        //   alert( $('#street').val());
        //  alert( address);

        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                $('#lat').val(latitude);
                $('#long').val(longitude);
                $('#formated_address_lat_long')
                .html('<div class="alert alert-success">' + results[0].formatted_address + '</div>')
                .fadeTo(100, 0.1).fadeTo(250, 1)
                .css({"position": "relative","top": "15px"});
                alert($('#lat').val(latitude));
            } else {
                alert($('#lat').val(latitude));
                alert($('#long').val(longitude));
                $('#formated_address_lat_long')
                .fadeTo(100, 0.1).fadeTo(250, 1)
                .html('<div class="alert alert-error">Address Not Found</div>')
                .css({"position": "relative","top": "15px"});
            }
        });
    });
});

我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果我包括以下行 这解决了问题

         <script type="text/javascript" src="maps.google.com.au/maps      /api/js?sensor=false"></…