简体   繁体   English

未捕获的TypeError:无法读取未定义的Google Map和jquery的属性'x'

[英]Uncaught TypeError: Cannot read property 'x' of undefined Google Map and jquery

I looked for many answers on this exception, but all solution that I found did not solve my problem. 我为此异常寻找了许多答案,但是我发现的所有解决方案都无法解决我的问题。

I'm using jQuery and google map. 我正在使用jQuery和Google Map。 I have html page contains some elements, and a .js file that contains many functions and ajax calls. 我的html页面包含一些元素,而.js文件包含许多功能和ajax调用。

In HTML: I place my script at the end of the body: 在HTML中:我将脚本放在正文的末尾:

<!-- here my .js file--> 
<script type="text/javascript" src="js/myFunctions.js"></script>
<!-- Google Map -->
<script src="https://maps.googleapis.com/maps/api/js?key=MyKey" async defer>
</script>
</body>

In myFunctions.js file: 在myFunctions.js文件中:

I have an ajax call that return the latitude and longitude which I send it to the function that create the map: 我有一个ajax调用,它返回纬度和经度,然后将其发送给创建地图的函数:

initMap(parseFloat($(this).find("latitude").text()),
        parseFloat($(this).find("longitude").text()));

Then I call the map function to create the map: 然后,我调用map函数来创建地图:

var map;
initMap = function (lat, lan) {
    console.log(lat);
    map = new google.maps.Map(document.getElementById('locMapDiv'), {
        center: {lat:lat , lng: lan},
        zoom: 13
    });
}

Everything is work fine, however in console it shows error when I drag on map: Uncaught TypeError: Cannot read property 'x' of undefined. 一切正常,但是在控制台上拖动时显示错误:Uncaught TypeError:无法读取未定义的属性“ x”。 I do not know why I got this error, while the drag is work fine. 当拖动工作正常时,我不知道为什么会出现此错误。

what could be the reason for this error? 该错误的原因可能是什么? because everything seems work fine even the drag and zoom it also work perfect. 因为即使拖动和缩放也可以完美工作,但一切似乎都可以正常工作。

Try to reverse the order of included scripts : 尝试颠倒所包含脚本的顺序:

<script src="https://maps.googleapis.com/maps/api/js?key=MyKey" async defer>
<script type="text/javascript" src="js/myFunctions.js"></script>

And try to call your init function inside a load function to make sure that the DOM is loaded completely : 并尝试在load函数中调用init函数,以确保DOM已完全加载:

google.maps.event.addDomListener(window, "load", , function () {
     initMap(parseFloat($(this).find("latitude").text()),
             parseFloat($(this).find("longitude").text()));
);

Hope this helps. 希望这可以帮助。

Try set center with object type LatLng. 尝试使用对象类型LatLng设置中心。

For your case, use the code below: 对于您的情况,请使用以下代码:

var centerLatLng = new google.maps.LatLng(lat, lan);

暂无
暂无

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

相关问题 Google Map错误“未捕获的TypeError:无法读取未定义的属性&#39;x&#39;” - Google Map Error “Uncaught TypeError: Cannot read property 'x' of undefined” 未捕获到的TypeError:无法读取Google Map API中未定义的属性“ PlacesService” - Uncaught TypeError: Cannot read property 'PlacesService' of undefined in google map api 未捕获的TypeError:无法读取未定义的属性“ x” - Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性x - Uncaught TypeError: Cannot read property x of undefined 未捕获的类型错误:无法使用谷歌地图(markerWithlabel js)读取未定义的属性“fitBounds” - Uncaught TypeError: Cannot read property 'fitBounds' of undefined with google map( markerWithlabel js) 未捕获的TypeError:无法读取未定义的Google Map V3的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined Google Map V3 未捕获的TypeError:无法使用React读取未定义的属性“map” - Uncaught TypeError: Cannot read property 'map' of undefined with React 未捕获的TypeError:无法读取未定义的传单地图ArcIGS的属性“ lat” - Uncaught TypeError: Cannot read property 'lat' of undefined Leaflet map ArcIGS reactjs-未捕获的TypeError:无法读取未定义的属性&#39;map&#39; - reactjs - Uncaught TypeError: Cannot read property 'map' of undefined 未捕获的类型错误:无法读取未定义的属性“映射”(REACT NATIVE - Javascript) - Uncaught TypeError: Cannot read property 'map' of undefined ( REACT NATIVE - Javascript )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM