Google 3.32 вызывает ошибку MapOverlay - PullRequest
0 голосов
/ 24 мая 2018

Все отлично работает с использованием 3.31, но когда я использую скрипт 3.32 или 3.33, мой оверлей не загружается.Вот мой класс наложения:

    function MapOverlay(bounds, image, map) {

        // Now initialize all properties.
        var sw = new google.maps.LatLng(bounds.southWest.latitude, bounds.southWest.longitude);
        var ne = new google.maps.LatLng(bounds.northEast.latitude, bounds.northEast.longitude);
         //if (westernCorner.longitude > easternCorner.longitude)
        this.bounds = new google.maps.LatLngBounds(sw, ne);
        this.image = image;
        this.map = map;

        // We define a property to hold the image's
        // div. We'll actually create this div
        // upon receipt of the add() method so we'll
        // leave it null for now.
        this.div = null;

        // Explicitly call setMap() on this overlay
        this.setMap(map);
    }

    MapOverlay.prototype = new google.maps.OverlayView();

    MapOverlay.prototype.onAdd = function() {

        // Note: an overlay's receipt of onAdd() indicates that
        // the map's panes are now available for attaching
        // the overlay to the map via the DOM.

        // Create the DIV and set some basic attributes.
        var div = document.createElement('DIV');
        div.style.border = "none";
        div.style.borderWidth = "0px";
        div.style.position = "absolute";

        // Create an IMG element and attach it to the DIV.
        var img = document.createElement("img");
        img.src = this.image;
        img.style.width = "100%";
        img.style.height = "100%";
        div.appendChild(img);

        // Set the overlay's div_ property to this DIV
        this.div = div;

        // We add an overlay to a map via one of the map's panes.
        // We'll add this overlay to the overlayImage pane.
        var panes = this.getPanes();
        panes.overlayLayer.appendChild(div);
    };

    MapOverlay.prototype.draw = function() {

        // Size and position the overlay. We use a southwest and northeast
        // position of the overlay to peg it to the correct position and size.
        // We need to retrieve the projection from this overlay to do this.
        var overlayProjection = this.getProjection();

        // Retrieve the southwest and northeast coordinates of this overlay
        // in latlngs and convert them to pixels coordinates.
        // We'll use these coordinates to resize the DIV.
        var sw = overlayProjection.fromLatLngToDivPixel(this.bounds.getSouthWest());
        var ne = overlayProjection.fromLatLngToDivPixel(this.bounds.getNorthEast());

        // Resize the image's DIV to fit the indicated dimensions.
        var div = this.div;
        div.style.left = sw.x + 'px';
        div.style.top = ne.y + 'px';
        console.log("ne.x: " + ne.x + " sw.x " + sw.x + " width: " + (ne.x - sw.x));
        div.style.width = (ne.x - sw.x) + 'px';
        div.style.height = (sw.y - ne.y) + 'px';
    };

    MapOverlay.prototype.onRemove = function() {
        this.div.parentNode.removeChild(this.div);
        this.div = null;
    };

    // Note that the visibility property must be a string enclosed in quotes
    MapOverlay.prototype.hide = function() {
        if (this.div) {
            this.div.style.visibility = "hidden";
        }
    };

    MapOverlay.prototype.show = function() {
        if (this.div) {
            this.div.style.visibility = "visible";
        }
    };

А вот где наложение настроено:

    addImageOverlay: function(bounds, url, hidden) {
        var ne = new google.maps.LatLng(bounds.northEast.latitude,
                bounds.northEast.longitude);
        var sw = new google.maps.LatLng(bounds.southWest.latitude,
                bounds.southWest.longitude);
        var b = new google.maps.LatLngBounds(sw, ne);

        if(this.imageOverlay){
            this.imageOverlay.setMap(null);
            this.imageOverlay = null;
        }

        this.imageOverlay = new MapOverlay(bounds, url, this.map);

        this.imageOverlay.setMap(this.map);
        if (hidden === true) {
            this.imageOverlay.setMap(null);
        } else {
            this.imageOverlay.setMap(this.map);
        }

        return this.imageOverlay;
    }

Я получаю ошибку:

    Cannot read property 'parentNode' of null
    at MapOverlay.onRemove (MapOverlay.js:90)

Из того, что я могу сказать, метод onAdd () не вызывается, когда я вызываю setMap (map).Следовательно, this.div имеет значение null при вызове onRemove () (таким образом, NPE).Карта заполнена и действительна, насколько я могу судить.Опять же, это происходит только в 3.32 и 3.33 API Карт Google.Я не могу найти никаких изменений в документации, которые могли бы вызвать это и застряли на один день.Сейчас я могу придерживаться 3.31, но якобы в августе закат.

1 Ответ

0 голосов
/ 16 июля 2018
MapOverlay.prototype.onRemove = function() {
    if (this.div && this.div.parentNode) {
        this.div.parentNode.removeChild(this.div);        
    }
    this.div = null;    
};

Добавлен блок IF, и это решит проблему.Пожалуйста, найдите ссылку ниже: - 1. https://github.com/googlemaps/v3-utility-library/issues/393 2. https://github.com/enyo/dropzone/issues/1083

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...