我很难过。这是我正在使用的代码:
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);
});
});
单击自动提示链接时,它应在第一个和第二个示例中对结果进行地理编码。但是,当我从下拉列表中单击autosuggest选项时,我在第39行得到以下错误:
未捕获的TypeError:无法调用未定义的方法'val'
有人可以帮忙查明我在哪里出错吗?
编辑:要复制,我会执行以下操作:
显然根据我的编辑第39行说:
if(ctryiso!==''){
答案 0 :(得分: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);
那么它会像这样工作,
的 UPDATE,强> 的
根据您的要求,这是一种更清洁的方式。 http://jsfiddle.net/Jfdbz/9/
像这样更改processLocation
,
processLocation = function (callback) { // accept a callback argument
var input = $("#loc"),
lat = $("#lat"),
long = $("#lng");
// rest of the cocde ***
}
答案 1 :(得分:0)
在这一行:
google.maps.event.addListener(autocomplete, 'place_changed', processLocation);
调用该回调时没有参数,因此input
未定义,因此您无法在第6行调用val()
。
而是这样做:
google.maps.event.addListener(autocomplete, 'place_changed',function(){
processLocation(arguments); //With whatever arguments you need
});
答案 2 :(得分: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");