admin管理员组文章数量:1426594
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. I have html page contains some elements, and a .js file that contains many functions and ajax calls.
In HTML: I place my script at the end of the body:
<!-- here my .js file-->
<script type="text/javascript" src="js/myFunctions.js"></script>
<!-- Google Map -->
<script src="" async defer>
</script>
</body>
In myFunctions.js file:
I have an ajax call that return the latitude and longitude which I send it to the function that create the map:
initMap(parseFloat($(this).find("latitude").text()),
parseFloat($(this).find("longitude").text()));
Then I call the map function to create the 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. 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.
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. I have html page contains some elements, and a .js file that contains many functions and ajax calls.
In HTML: I place my script at the end of the body:
<!-- here my .js file-->
<script type="text/javascript" src="js/myFunctions.js"></script>
<!-- Google Map -->
<script src="https://maps.googleapis./maps/api/js?key=MyKey" async defer>
</script>
</body>
In myFunctions.js file:
I have an ajax call that return the latitude and longitude which I send it to the function that create the map:
initMap(parseFloat($(this).find("latitude").text()),
parseFloat($(this).find("longitude").text()));
Then I call the map function to create the 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. 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.
Share Improve this question edited Dec 13, 2015 at 22:35 F. Fo asked Dec 12, 2015 at 23:31 F. FoF. Fo 1237 silver badges18 bronze badges 1- 2 Possible duplicate of Detecting an undefined object property – Liam Commented Nov 20, 2018 at 12:04
2 Answers
Reset to default 2Try to reverse the order of included scripts :
<script src="https://maps.googleapis./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 pletely :
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.
For your case, use the code below:
var centerLatLng = new google.maps.LatLng(lat, lan);
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read property 'x' of undefined Google Map and jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483761a2660290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论