0

我很难受。这是我正在使用的代码:

http://jsfiddle.net/arunpjohny/Jfdbz/

$(function () {
    var lastQuery  = null,
        lastResult = null, // new!
        autocomplete,
        processLocation = function (input, lat, long, callback) { // accept a callback argument
            var query = $.trim(input.val()),
                geocoder;

            // if query is empty or the same as last time...
            if (!query || query === lastQuery) {
                if (callback) {
                    callback(lastResult); // send the same result as before
                }
                return; // and stop here
            }

            lastQuery = query; // store for next time

            geocoder = new google.maps.Geocoder();
            geocoder.geocode({address: query}, function (results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    lat.val(results[0].geometry.location.lat());
                    long.val(results[0].geometry.location.lng());
                    lastResult = true; // success!
                } else {
                    alert("Sorry - We couldn't find this location. Please try an alternative");
                    lastResult = false; // failure!
                }
                if (callback) {
                    callback(lastResult); // send the result back
                }
            });
        },
        ctryiso = $("#ctry").val(),
        options = {
            types: ["geocode"]
        };

    if (ctryiso !== '') {
        options.componentRestrictions= { 'country': ctryiso };        
    }
    autocomplete = new google.maps.places.Autocomplete($("#loc")[0], options);

    google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

    $('#search').click(function (e) {
        var form = $(this).closest('form'),
            input = $("#loc"),
            lat = $("#lat"),
            lng = $("#lng");
        e.preventDefault(); // stop the submission

        processLocation(input, lat, lng, function (success) {
            if (success) { // if the geocoding succeeded, submit the form
                form.submit();
            }
        });
    });

    $('#geosearch').click(function (e) {
        var form = $(this).closest('form'),
            input = $("#geoloc"),
            lat = $("#geolat"),
            lng = $("#geolng");
        e.preventDefault(); // stop the submission

        processLocation(input, lat, lng);
    });
});

单击自动建议链接时,它应该对第一个和第二个示例中的结果进行地理编码。但是,当我从下拉列表中单击自动建议选项时,我在第 39 行收到以下错误:

未捕获的类型错误:无法调用未定义的方法“val”

任何人都可以帮助查明我在这里出错的地方吗?

编辑:要复制我执行以下操作:

  • 在 Chrome 中打开 javascript 控制台
  • 在顶行的位置框中输入几个字母
  • 从下拉菜单中单击建议的位置

显然根据我的编辑第 39 行是这样说的:

if (ctryiso !== '') {

4

3 回答 3

1

google.maps.event.addListener不会将参数传递给您提供的函数(processLocation),因此您需要为其创建新函数。

process = function () {
    var input = $("#loc"),
        lat = $("#lat"),
        lng = $("#lng");
    processLocation(input, lat, lng, null);
}

并称之为,

google.maps.event.addListener(autocomplete, 'place_changed', process);

然后它会像这样工作,

http://jsfiddle.net/Jfdbz/6/

更新,


根据您的要求,这是一种更清洁的方式。 http://jsfiddle.net/Jfdbz/9/

改成processLocation这样,

processLocation = function (callback) { // accept a callback argument
            var input = $("#loc"),
                lat = $("#lat"),
                long = $("#lng");
            // rest of the cocde ***
}
于 2013-06-20T14:28:34.500 回答
0

在这一行:

google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

该回调在没有参数的情况下被调用,因此input未定义,因此您不能val()在第 6 行调用它。

而是这样做:

google.maps.event.addListener(autocomplete, 'place_changed',function(){
    processLocation(arguments); //With whatever arguments you need
});
于 2013-06-20T14:24:18.663 回答
0

据我所知,这

$('#search').click(function (e) {
    var form = $(this).closest('form'),
        input = $("#loc"),
        lat = $("#lat"),
        lng = $("#lng");
    e.preventDefault(); // stop the submission

    processLocation(input, lat, lng, function (success) {
        if (success) { // if the geocoding succeeded, submit the form
            form.submit();
        }
    });
});

使用范围函数定义 lat、lng 等变量,因此您不能在其他函数中引用这些变量,例如

 geocoder.geocode({address: query}, function (results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                lat.val(results[0].geometry.location.lat());
                long.val(results[0].geometry.location.lng());

尝试在外面定义它们:

 var lastQuery  = null,
    lastResult = null, // new!
    autocomplete,
    lat = null,
    lng = null ...

然后像这样使用:

var form = $(this).closest('form'),
    input = $("#loc"); // !!
// no var here:
    lat = $("#lat");
    lng = $("#lng");
于 2013-06-20T14:25:59.417 回答