我的网站有问题。在提交表单之前,我使用自动完成功能来查找地址的经纬度。问题是,在我有 2 个实例的页面上,我在 Chrome 中收到错误“'geometry' is null or not an object”,第一个实例不会得到 latlon。
if($('input#searchArea').length){
var input = document.getElementById('searchArea');
var options = {
componentRestrictions: {country: 'uk'}
}
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function(){
var place = autocomplete.getPlace();
var location = String(place.geometry.location); // The problemed line
var latLong = location.substr(1, location.length-2);
latLong = latLong.split(', ');
if($('input#searchLatLong').length == 0) $('form#findArea').append('<input type="hidden" name="ll" id="searchLatLong" />');
$('input#searchLatLong').val(latLong[0] + ',' + latLong[1]);
});
}
if($('input#letArea').length){
var input = document.getElementById('letArea');
var options = {
componentRestrictions: {country: 'uk'}
}
autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function(){
var place = autocomplete.getPlace();
var location = String(place.geometry.location);
var latLong = location.substr(1, location.length-2);
latLong = latLong.split(', ');
if($('input#letLatLong').length == 0) $('form#letPlaceArea').append('<input type="hidden" name="ll" id="letLatLong" />');
$('input#letLatLong').val(latLong[0] + ',' + latLong[1]);
});
}
有没有人遇到过这个?
干杯, RJ
编辑:发现问题。变量的名称相同。在第二个实例上更改它们对其进行排序。
干杯, RJ