dtpozner
dtpozner

Reputation: 301

you have included the Google Maps API multiple times on this page. This may cause unexpected errors

I get two errors when using google maps API:

  1. Warning: you have included the Google Maps API multiple times on this page. This may cause unexpected errors.
  2. a is null (main.js)

I don't know if there is any connection between the two, but the final result is that my page cannot load the map.

Here is my head tag:

<script type="text/javascript" src="../s/Jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="../s/jquery.placeholder.min.js"></script>
<script type="text/javascript" src="../s/index.js"></script>
<script type="text/javascript" src="../s/Jquery/ui/jquery.ui.core.js"></script>
<script type="text/javascript" src="../s/Jquery/ui/jquery.ui.widget.js"></script>

<script type="text/javascript" src="../s/Jquery/ui/jquery.ui.position.js"></script>
<script type="text/javascript" src="../s/Jquery/ui/jquery.ui.autocomplete.js"></script>
<script src="../dd/markerclusterer_compiled.js" type="text/javascript"></script><script src="../dd/dealers_js.js" type="text/javascript"></script><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

Here is the function which I call the google map:

function set_google_map(set_region)
{
    
        var zoom;
        var center;
        switch (set_region) {
            case "usa":
                zoom = 3;
                center = new google.maps.LatLng(37.09, -95.71);
                break;
            case "europe":
                zoom = 3;
                center = new google.maps.LatLng(48.58, 7.71);
                break;
            case "east":
                zoom = 3;
                center = new google.maps.LatLng(31, 121);
                break;
            default:
                zoom = 1;
                center = new google.maps.LatLng(35, 5);
        }
        // Creating the map  
        map = new google.maps.Map(document.getElementById('map'), options);
        google.maps.event.addListener(map, 'click', function(){
            clean_icons();
            if (infowindow) 
                infowindow.close();
        });
        map.setCenter(center);
        map.setZoom(zoom);
    
    
            //bounds = new google.maps.LatLngBounds();  
}

These are my variables:

var UseridEncr;
var super_category="ATE";
var infowindow;
var map;
var bounds;
var image="../dd/i/green.png";
var image_active="../dd/i/red.png";
var mc ;
var location_changed=true;
var current_region="usa";
var mcOptions = {gridSize:30, maxZoom: 8};
var last_marker;
var last_marker_z;
var DealersData ;
var markers=[];
var markers_selected=[];
claster_markers=[];
var current_view=0;
myData={};
var selected_items=[];
var total_selected=0;

Thanks for your prompt answer.

Upvotes: 25

Views: 86900

Answers (5)

david strachan
david strachan

Reputation: 7228

Do any of these files include the map API?

<script src="../dd/markerclusterer_compiled.js" type="text/javascript"></script>
<script src="../dd/dealers_js.js" type="text/javascript">

Upvotes: 5

zhuhang.jasper
zhuhang.jasper

Reputation: 4475

For those who append Google Map script to DOM on runtime, you should remove traces of Google Map before you re-append the next time.

You can unload google map script by following 2 steps:

  • Remove google map scripts from DOM.
  • Remove google from BOM (window object).

Example


declare var window;


function removeGoogleMapScript() {
    console.debug('removing google script...');
    let keywords = ['maps.googleapis'];

    //Remove google from BOM (window object)
    window.google = undefined;

    //Remove google map scripts from DOM
    let scripts = document.head.getElementsByTagName("script");
    for (let i = scripts.length - 1; i >= 0; i--) {
        let scriptSource = scripts[i].getAttribute('src');
        if (scriptSource != null) {
            if (keywords.filter(item => scriptSource.includes(item)).length) {
                scripts[i].remove();
                // scripts[i].parentNode.removeChild(scripts[i]);
            }
        }
    }
}


function addGoogleMapScript() {
    removeGoogleMapScript();
    console.debug('adding google script...');
    let dynamicScripts = [`https://maps.googleapis.com/maps/api/js?v=quarterly&key=123yourApiKey`];
    for (let i = 0; i < dynamicScripts.length; i++) {
        let node = document.createElement('script');
        node.src = dynamicScripts[i];
        node.type = 'text/javascript';
        node.async = false;
        node.charset = 'utf-8';
        node.onload = uponFinishLoadingScript; //probably to initialise your map or something
        document.head.appendChild(node);
    }
}

Upvotes: 12

Muganwas
Muganwas

Reputation: 21

I had the same exact problem, I finally found the code that was causing the problem within the functions.php file.

// Custom scripts ans styles //

wp_enqueue_script( 'wpjobus-google-maps-script', 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false', array( 'jquery' ), '2013-07-18', true );

i added the api key to the above. The code below was the final result.

// Custom scripts ans styles // wp_enqueue_script( 'wpjobus-google-maps-script', 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=MY_API_KEY', array( 'jquery' ), '2013-07-18', true );

I then deleted the script I had earlier added to the header of the page that had the map and voila!

Upvotes: 2

Jim Buckley Barret
Jim Buckley Barret

Reputation: 330

What do you have defined for "options"

map = new google.maps.Map(document.getElementById('map'), **options**);

I don't see it in variable list.

Search for all locations of (presuming you haven't made a local copy). I believe you probably do have it added to dealers_js.js or a another file that dealers includes.

Jim

Upvotes: 0

M. Ahsan
M. Ahsan

Reputation: 171

I faced the same type of problem. This occurs if your web page including maps api more than one time.

I have checked in my case there was a .js file that was also calling maps api, so please first check if you are including maps api more than one time, if you are, remove the one.

Upvotes: 17

Related Questions