我无法让我的地理编码在我的某个jsp页面中工作。它在另一个工作,但这是造成麻烦。Uncaught TypeError:无法读取未定义的属性'geocode'
我有一个base.jsp在我所有的jsp页面之间共享。其中有:
var geocoder;
$(document).ready(function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center : { lat : 56.138, lng : 8.967 },
zoom : 7,
disableDoubleClickZoom : true
});
geocoder = new google.maps.Geocoder();
});
function addressTolatlng(address){
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();
var myLatlng = new google.maps.LatLng(latitude, longitude);
console.log(myLatlng);
}
});
}
这是我的jsp页面,其提供了错误:
$(document).ready(function() {
console.log(input);
var input = document.getElementById('addressField');
var autocomplete = new google.maps.places.Autocomplete(input, {
types: ["geocode"]
});
autocomplete.bindTo('bounds', map);
});
<input type="text" placeholder="address" name="address" id="addressField" class="form-control" aria-label="..." value="">
<script>
$("#addressField").keyup(function() {
var address = document.getElementById('addressField').value;
addressTolatlng(address);
});
</script>
我得到遗漏的类型错误:运行此时无法读取的未定义的属性,“地址解析”。但是我得到了另一个具有相同设置的页面,并且它的工作原理非常完美,所以对于我来说,这种方式不会有相同的表现。
控制台也给出了这些:
(anonymous function) @ eventHandler?action=createEvent:790
m.event.dispatch @ jquery-1.11.3.min.js:4
r.handle @ jquery-1.11.3.min.js:4
我有很多剧本进口,这对我来说是页面上的唯一区别。这会导致问题吗?
你有匿名函数,地理编码是在它外面定义的,所以它不会被识别。这意味着它将返回为未定义。 –
我可以看到控制台也给出了一个匿名函数的提示。但是这在另一个页面中工作,它怎么会在这里不起作用? – JonCode
基本上和我说的Danial解释的一样。您必须清除并且如果要将变量传递到函数中,则需要在此函数的参数中设置该变量。例如:'function your_function($ a,$ b,$ c){console.log($ a); // <不再未定义:)}' –