简体   繁体   中英

“a is null” even after Google map is successfully loaded

I have a set of jQuery UI tabs that each load project.php using ajax. Depending on the parameters passed to the script, a different Google map is displayed using the following JavaScript inside project.php:

var tab_index = $('#tabs').tabs('option', 'selected');
$('.site_map:visible').css('height','300px');

MapID = $('.site_map:visible').attr('id');

if (MapID !== 'map-new'){
    var map_id = 'map-'+tab_index;
    $('.site_map:visible').attr('id', map_id);
} else {
    MapNewSite();
}

var latlng = new google.maps.LatLng(19,-70.4);
var myOptions = {
  zoom: 8,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};

arrMaps[tab_index] = new google.maps.Map(document.getElementById("map-" + tab_index), myOptions);
arrInfoWindows[tab_index] = new google.maps.InfoWindow();
placeMarker($('.site_details:visible .inpLat').val(), $('.site_details:visible .inpLng').val(), tab_index);

function MapNewSite(){
    arrMaps[tab_index] = new google.maps.Map(document.getElementById("map-new"), myOptions);
    placeMarker(19,-70.4,tab_index);
    arrInfoWindows[tab_index] = new google.maps.InfoWindow();
}

Each map loaded using parameters returned by a query of my database loads without any problems. However, in one last instance, I load project.php in a tab without any parameters so as to have a blank tab for users to manipulate. The signal that the map is not to be loaded using database coordinates is that the id of its div is "map-new".

The map generated in this tab loads, but then gives me the "a is null" error which usually means it couldn't find a div with the id specified to initialize the map. What is causing this error even after the map has loaded? How do I stop the error from occurring?

Here is the JavaScript in the parent page containing the tab site:

    var arrMaps = {};
    var arrInfoWindows = {};
    var arrMarkers = {};

    function placeMarker(lat, lng, tab_index){
        map = arrMaps[tab_index];
        var bounds = new google.maps.LatLngBounds();
        var latlng = new google.maps.LatLng(
            parseFloat(lat),
            parseFloat(lng)
        );

        bounds.extend(latlng);
        createMarker(latlng, tab_index);
        map.fitBounds(bounds);

        zoomChangeBoundsListener = 
            google.maps.event.addListener(map, 'bounds_changed', function(event) {
                if (this.getZoom()){
                    this.setZoom(10);
                }
            google.maps.event.removeListener(zoomChangeBoundsListener);
        });
    }

    function createMarker(latlng, tab_index) {
        var html = '<a href="#" target="_blank" onclick="OpenMapDialog();return false;">Click here to move marker</a>';   
        arrMarkers[tab_index] = new google.maps.Marker({
            map: arrMaps[tab_index],
            position: latlng
        });

        arrInfoWindows[tab_index] = new google.maps.InfoWindow();

        google.maps.event.addListener(arrMarkers[tab_index], 'click', function() {
            arrInfoWindows[tab_index].setContent(html);
            arrInfoWindows[tab_index].open(arrMaps[tab_index], arrMarkers[tab_index]);
        });
    }

    $(function() {
        $( "#tabs" ).tabs({
            ajaxOptions: {
                error: function( xhr, status, index, anchor ) {
                    $( anchor.hash ).html(
                        "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                        "If this wouldn't be a demo." );
                }
            },
            cache: true
        });
    });

Take a look to http://www.pittss.lv/jquery/gomap/ . Easy to use and very powerful. I myself use it.

原来,我是在if内部和外部意外地初始化了地图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM