简体   繁体   English

Google Map错误“未捕获的TypeError:无法读取未定义的属性'x'”

[英]Google Map Error “Uncaught TypeError: Cannot read property 'x' of undefined”

I am working with Google map and I wrote a JS code like below and it works perfectly when a user gives location realizing permission by Browser, but when he blocks the permission, the map doesn't show anything and wen I click on its area I get the following error:\\ 我正在使用Google地图,并且编写了如下所示的JS代码,当用户通过浏览器提供位置实现权限时,该代码可完美运行,但是当他阻止该权限时,该地图将不会显示任何内容,因此我单击了该区域出现以下错误:\\

Error: 错误:

Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性“ x”

JS SCRIPT: JS脚本:

    var pos = new Object();
    var marker1 = new Object();
    var marker2 = new Object();
    var resp;
    var loc;
    var bus_loc;

    function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
            //        zoom: 13
        });

        var infoWindow = new google.maps.InfoWindow({});
        var scope = angular.element($("#address")).scope();
        var angLoc;
        scope.$apply(function () {
            angLoc = scope.contact.business.location.split(',');
            bus_loc = {
                lat: parseFloat(angLoc[0]),
                lng: parseFloat(angLoc[1])
            };
        });
        var image = '/static/image/main_template/map/2.png';
        marker2 = new google.maps.Marker({
            map: map,
            position: bus_loc,
            icon: image
        });
        marker2.addListener('click', function() {
            infoWindow.setPosition(bus_loc);
            infoWindow.setContent('<p style="padding: 0; margin-top: 10px; direction: rtl">ساینا</p>');
            infoWindow.open(map, marker2);
        });

        map.setCenter(bus_loc);
        // Try HTML5 geolocation.
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                pos = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };

                loc = {
                    lat: parseFloat(pos.lat),
                    lng: parseFloat(pos.lng)
                };
                var image = '/static/image/main_template/map/1.png';
                marker1 = new google.maps.Marker({
                    map: map,
                    position: loc,
                    icon: image
                });
                marker1.addListener('click', function() {
                    infoWindow.setPosition(loc);
                    infoWindow.setContent('<p style="padding: 0; margin-top: 10px; direction: rtl">اینجا هستید</p>');
                    infoWindow.open(map, marker1);
                });
                map.setCenter(loc);

                var distanceService = new google.maps.DistanceMatrixService();
                distanceService.getDistanceMatrix({
                        origins: ['' + loc.lat + ',' + loc.lng + ''],
                        destinations: ['' + bus_loc.lat + ',' + bus_loc.lng + ''],
                        travelMode: google.maps.TravelMode.DRIVING,
                        unitSystem: google.maps.UnitSystem.METRIC,
                        durationInTraffic: true,
                        avoidHighways: false,
                        avoidTolls: false
                    },
                    function(response, status) {
                        if (status !== google.maps.DistanceMatrixStatus.OK) {
                            console.log('Error:', status);
                        } else {
                            resp = response;
                            $('#distance').html('<b>فاصله از شما: <b>' + resp.rows[0].elements[0].distance.text);
                            $('#duration').html('<b>زمان تخمینی رسیدن شما به این با خودرو: <b>' + resp.rows[0].elements[0].duration.text);

                        }
                    });



                var bounds = new google.maps.LatLngBounds();
                bounds.extend(marker1.getPosition());
                bounds.extend(marker2.getPosition());

                map.fitBounds(bounds);


            }, function() {
                handleLocationError(true, infoWindow, map.getCenter());
            });
        } else {
            // Browser doesn't support Geolocation
            handleLocationError(false, infoWindow, map.getCenter());
        }




    }

    function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        //    infoWindow.setPosition(pos);
        //    infoWindow.setContent(browserHasGeolocation ?
        //        'مکان جغرافیایی یافت نشد' :
        //        'مرورگر شما امکان نشان دادن مکان جغرافیایی را ندارد.');
    }

What should I do? 我该怎么办?

不要像这样添加任何原型继承,谷歌地图渲染有问题

    Object.prototype.boolToArray = function () {}

Make sure you're not overriding 'window.self'! 确保您没有覆盖“ window.self”!

Had the same problem, when I put a break point where the error was thrown in common.js, I saw that it was checking window.self, and window.self did not = window! 遇到同样的问题,当我在common.js中放置一个引发错误的断点时,我看到它正在检查window.self,而window.self不等于window!

My issue: I had created a component and wrote "self = this" instead of "var self = this". 我的问题:我创建了一个组件,并写了“ self = this”而不是“ var self = this”。 I overwrote window.self which caused this error. 我改写了导致此错误的window.self。

This problem is usually due to the map div not being rendered before the javascript runs that needs to access it. 此问题通常是由于在需要运行JavaScript的javascript运行之前未渲染map div引起的。 you can find answer to this question here in this question 您可以在此问题中找到此问题的答案

I had the same issue and in my case it was because the IE or the IIS Server blocks some google Maps URLs. 我遇到了同样的问题,就我而言,这是因为IE或IIS服务器阻止了一些Google Maps URL。 You can check it using your developer tools F12 in Network tab and load the page with and without permission. 您可以使用“网络”选项卡中的开发人员工具F12进行检查,并可以在未经许可的情况下加载页面。 It's probable that you found a google Maps URL with status 4XX. 您很可能找到了状态为4XX的Google Maps URL。 I solved adding the google maps URLs to the trusted sites 我解决了将Google Maps URL添加到受信任的站点的问题

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

相关问题 未捕获的TypeError:无法读取未定义的Google Map和jquery的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined Google Map and jquery 未捕获到的TypeError:无法读取Google Map API中未定义的属性“ PlacesService” - Uncaught TypeError: Cannot read property 'PlacesService' of undefined in google map api 错误:未被捕获的TypeError:无法读取未定义的属性“映射” - Error: Uncaught TypeError: Cannot read property 'map' of undefined Javascript中的数组错误:未捕获的TypeError:无法读取未定义的属性&#39;x&#39; - Array Error in Javascript: Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性“ x” - Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性x - Uncaught TypeError: Cannot read property x of undefined 未捕获的错误:无法读取未定义的属性 x - Uncaught error: cannot read property x of undefined TypeError:无法读取未定义的属性“地图”错误 - TypeError: Cannot read property 'map' of undefined" error 错误TypeError:无法读取未定义的属性X - ERROR TypeError: Cannot read property X of undefined 未捕获的类型错误:无法使用谷歌地图(markerWithlabel js)读取未定义的属性“fitBounds” - Uncaught TypeError: Cannot read property 'fitBounds' of undefined with google map( markerWithlabel js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM