简体   繁体   English

未捕获的TypeError:无法读取未定义的属性“地理编码”

[英]Uncaught TypeError: Cannot read property 'geocode' of undefined

I cannot get my geocoding to work in one of my jsp pages. 我无法在一个jsp页面中使用地理编码。 It's working in another but this one is causing trouble. 它在另一个环境中工作,但是这引起了麻烦。

I have a base.jsp which is shared among all my jsp pages. 我有一个base.jsp,在我所有的jsp页面之间共享。 which has: 其中有:

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);

          } 
    }); 
}

This is my jsp page which gives the error: 这是我的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>

I get Uncaught TypeError: Cannot read property 'geocode' of undefined when running this. 我收到未捕获的TypeError:运行此代码时无法读取未定义的属性“地理编码”。 But I got another page which has the same setup, and there it works perfectly, so its kind odd for me how this one won't behave the same way. 但是我得到了另一个具有相同设置的页面,并且该页面可以完美地工作,所以这对我来说是不一样的。

the console also gives these: 控制台还提供以下功能:

(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

I have alot of script imports, which for me is the only difference on the pages. 我有很多脚本导入,对我来说,这是页面上的唯一区别。 Can this cause the problem? 这会引起问题吗?

In this line of code 在这行代码中

geocoder.geocode( { 'address': address}, function(results, status) {

geocoder is undefined (or null) and nothing can be accessed in it b/c it doesn't know what geocoder is. geocoder是未定义的(或为null),并且在b / c中无法访问任何内容,因为它不知道什么是geocoder。

The reason it is undefined is that geocoder is outside the scope of your addressTolatlng function. 不确定的原因是地址解析器不在addressTolatlng函数的范围内。 To fix this you can a) pass in geocoder to your function through a parameter b) define geocoder again in that function or c) add the function to your iniatilize() function (but I'm not 100% on that one) 要解决此问题,您可以a)通过参数将geocoder传递给您的函数b)在该函数中再次定义geocoder或c)将函数添加到iniatilize()函数中(但我不是100%使用该函数)

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取&#39;未捕获的TypeError:无法读取属性&#39;地理编码&#39;未定义&#39;错误 - Getting 'Uncaught TypeError: Cannot read property 'geocode' of undefined ' error 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性“ 1” - Uncaught TypeError: Cannot read property '1' of undefined 未捕获的TypeError:无法读取未定义的属性“格式” - Uncaught TypeError: Cannot read property 'format' of undefined 未捕获的TypeError:无法读取未定义的属性“属性” - Uncaught TypeError: Cannot read property 'attributes' of undefined JSON-未捕获的TypeError:无法读取未定义的属性 - JSON - Uncaught TypeError: Cannot read property of undefined 未捕获的TypeError:无法读取未定义的属性foo - Uncaught TypeError: Cannot read property foo of undefined 未捕获的typeerror无法读取未定义的属性“ split” - uncaught typeerror cannot read property 'split' of undefined 未被捕获的TypeError:无法读取未定义的属性“ form” - Uncaught TypeError: Cannot read property 'form' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;fn&#39; - Uncaught TypeError: Cannot read property 'fn' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM