You won't be able to view over 15 KML layers on a page that relies on Google Maps API

I've encountered an unusual issue: I need to create multiple KML layers from separate KML files and display them on a single map window. The number of files ranges from just a couple to less than fifty. Currently, my code works perfectly when given 1, 4, or 15 KML files (as URIs) in an array. So, it's safe to say that the code is functional and the KML files are formatted correctly.

For example, here's how I initialize my map with an array containing 23 KML URIs:

<body id="body" onload="initmap(new Array('https://CENCORED/kml/project64.kml', 'https://CENCORED/kml/project65.kml', 'https://CENCORED/kml/project66.kml', 'https://CENCORED/kml/project67.kml', 'https://CENCORED/kml/project69.kml', 'https://CENCORED/kml/project70.kml', 'https://CENCORED/kml/project71.kml', 'https://CENCORED/kml/project72.kml', 'https://CENCORED/kml/project75.kml', 'https://CENCORED/kml/project76.kml', 'https://CENCORED/kml/project80.kml', 'https://CENCORED/kml/project81.kml', 'https://CENCORED/kml/project82.kml...

However, issues arise when providing the code with an array of sixteen (16) or more KML URIs. In these cases, the KML files fail to render on the map canvas, even though there are no visible errors. How do I know this? Well, although the files may not be rendering visually, the InfoWindows associated with each KML file still appear when clicked, indicating that they exist within the map but are not displaying as intended.

Below is the complete code from my map_display.js file, which includes the initmap() function that is being called:

function initmap(urls){
    // Creating an option object for the map
    var myLatlng = new google.maps.LatLng(63.349501, 26.817627);
    var options = {
        zoom: 6,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // Initializing the map
    var map = new google.maps.Map(document.getElementById('map_canvas'), options);

    if(urls != null) {
        for(var i=0;i<urls.length;i++) {
            var url = urls[i];
            url = url+"?dummy="+(new Date()).getTime();
            var ctaLayer = createKML(url);
            ctaLayer.setMap(map);
        }
    }

    function createKML(url){

        var ctaLayer = new google.maps.KmlLayer(url, {suppressInfoWindows: true, preserveViewport: true});

        // Creating a correct reference for project edit URL
        var editUrl = urls[i];
        var s1 = editUrl.indexOf("project");
        s1 = s1+7;
        var s2 = editUrl.indexOf(".kml");
        editUrl = editUrl.substring(s1, s2);

        var baseUrl = getbaseUrl();

        var infoItems = new Array();
        infoItems = getInfo(editUrl);

        editUrl = '<b>' + infoItems[1] + '</b><br />' + infoItems[0] + '<br /><br /><a href="' + baseUrl + '/frontend/viewproject/' + editUrl + '">Katso projektin tiedot</a>';
        // Creating an InfoWindow object
        var infowindow = new google.maps.InfoWindow({ content: editUrl });

        google.maps.event.addListener(ctaLayer, 'click', function(kmlEvent) {
            var clickPos = kmlEvent.latLng;
            var posX = new google.maps.LatLng(clickPos.lat(), clickPos.lng());

            infowindow.close();
            infowindow.setPosition(posX);
            infowindow.open(map);

        });

        return ctaLayer;
    }

    function getbaseUrl(){

        var baseUrl = "https://" + window.location.hostname;
        var firstpath = window.location.pathname;
        var first_slash = firstpath.indexOf("/", 1);
        firstpath = firstpath.substring(0, first_slash);
        baseUrl = baseUrl + firstpath;

        return baseUrl;
    }

    function getInfo(pid){

        var jsoninfo = new Array();

        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        var json_location = getbaseUrl() + '/frontend/project_json/' + pid;

        xmlhttp.open("GET",json_location,false);
        xmlhttp.send();

        var json_answer = eval('(' + xmlhttp.responseText + ')');

        jsoninfo[0] = json_answer["projectName"];
        jsoninfo[1] = json_answer["builder"];

        return jsoninfo;
    }

}

I'm seeking some assistance. Unfortunately, I can't provide a live system for reference as it's password-protected and part of a larger page.

Answer №1

My experience with the default KML layer provided by Google has been somewhat limited in terms of success. For a better option, I suggest giving GeoXML3 or geoxml-v3 (which is not the same project) a try. Personally, I have used GeoXML3 to create a campus map and also experimented with creating my own educational hello world GeoXML3 map on Github.

Answer №2

KML layers function by incorporating the URLs of the KML files within the URL of each tile. When multiple KML layers are added, the cumulative length of the tile URLs exceeds 2048 characters, which is the maximum limit for URLs. To address this issue, consider shortening the URLs of the KML layers in use.

Answer №3

According to information found on Google's official documentation:

The Google Map has a restriction on the number of KML Layers that can be visible at once. If this limit is surpassed, none of your layers will show up on the map. The limitation is determined by the combined length of all URLs sent to the KMLLayer class, meaning it might differ for each application. On an average basis, you should aim to load around 10 to 20 layers before reaching this imposed threshold.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Loop through an array of div IDs and update their CSS styles individually

How can I iterate through an array of Div IDs and change their CSS (background color) one after the other instead of all at once? I have tried looping through the array, but the CSS is applied simultaneously to all the divs. Any tips on how to delay the ...

Monitoring Clicks within an Iframe

Is there a way to track a click event on a hyperlink within an iframe using Google Analytics? The iframe is located within the same domain as the main page. How can this be achieved? The iframe is added dynamically to the page after it has loaded. Is i ...

Maintaining a consistent style input until it is modified

I'm currently dealing with this code (continuing from a previous question): input[type=submit]:focus { background-color: yellow; outline: none; } The issue I'm facing is that when I click anywhere else on the screen, the background color go ...

Populate an ng-repeat table again while maintaining its original sorting order

Incorporating a table filled with data fetched from a webservice using the ng-repeat directive, I have enabled sorting functionality for all columns. Additionally, at regular intervals, the application polls the webservice to retrieve updated data. This o ...

Using React to implement MUI autocomplete feature alongside a MUI form

Recently, I have been utilizing a MUI form structured in the following manner: <Box component="form" onSubmit={event => { return handleSubmit(event); }} noValidate sx={{mt: 1}}> <TextField margin="normal" ...

Why is React's nested routing failing to render properly?

click here for image portrayal I am currently attempting to integrate react router, specifically a nested router. However, when I click the links on the contact page, no results are being displayed. Any assistance would be greatly appreciated. For more in ...

Tips for making a rounded bottom image slider with react-native?

Is there a way to design an image slider similar to this with rounded bottom images? ...

Partially extended texture in Three.js

Currently, I am using the Collada loader in Three.js r65 to load my 3D object. Upon loading, I apply a texture to all parts of the model using the following code snippet. var loader = new THREE.ColladaLoader(); loader.options.convertUpAxis = true; loader ...

Is a 'Virtual DOM' included in React Native's architecture?

According to the ReactJS wiki page on Virtual DOM: React uses an in-memory cache of data structures to efficiently compute differences and update the displayed DOM in the browser. This allows developers to write code as if the entire page is re-rendered ...

Searching for corresponding items in multi-dimensional arrays using Javascript

For my project in Javascript, I am facing the challenge of matching entire arrays. In this scenario, I have a userInput array and my goal is to locate a similar array within a multi-dimensional array and display the match. var t1 = [0,0,0]; var t2 = [1,0, ...

Looping through multiple JSON requests using $.getJSON without the use of the let statement to ensure cross

Currently, I am developing a web application that loads JSON files into leaflet layers based on user actions. The code snippet below successfully accomplishes this task when using the "let" statement. However, removing it results in variables from the last ...

Mastering the Art of Incorporating jQuery, JavaScript and CSS References into Bootstrap with ASP.NET WebForms

Hey there, I'm currently working on a personal project as a beginner in Bootstrap. My main challenge for the past two days has been trying to integrate a dateTimePicker and number Incrementer using Bootstrap techniques. Despite my efforts in researchi ...

Achieving a Transparent Flash overlay on a website without hindering its usability (attention, interaction, form submissions, etc.)

Currently, we are attempting to overlay a transparent flash on top of an iframe which loads external websites. Is there a method to configure the page in a way that allows the transparent flash to be displayed while still allowing interaction with the und ...

Encountered issues loading JavaScript and received a pyppeteer error while trying to access a website through requests

I am facing a challenge when trying to scrape a webpage post login using BeautifulSoup and requests. Initially, I encountered a roadblock where the page requested JavaScript to be enabled to continue using the application. To work around this issue, I de ...

Whenever a user logs in or logs out from the child component, the app.js is not being re-rendered

I'm having trouble figuring out how to re-render the app.js function. It loads initially, but when I click the login or logout button, I need to call a function from the helper again to check the user status. Here is the code snippet for App.js: impor ...

Assorted presentation of list items in HTML tags

I am working on creating a poll and I was wondering if there is a way to display the questions randomly each time the page is visited. I'm thinking of storing the questions in a PHP or JavaScript array. Can anyone recommend a good tutorial that can he ...

What techniques can you leverage in React to iterate through nested arrays, such as using .map or other alternatives?

As part of my work, I often come across an Array structure similar to the one below: Array = [ { product1, orderedBy = [{customer1}, {customer2},.....,{customerN}] }, { product2, ...

Using a conditional statement in JavaScript, create a mapping between the values in an array and string

I have a dropdown list that I want to populate with options. The functionality of the onchange event is handled by the following code snippet: const handleChange = (event) => { onFilterChange(filterName, event.target.value); } The value of event.ta ...

The button is converting my text to a string instead of the integer format that I require

Hello everyone, I've been grappling with this button conundrum for the last 45 minutes, and I can't seem to find a solution. I have created three different buttons in my code snippet below. (html) <div class="action"> ...

What is the procedure for eliminating an event listener that does not directly invoke a function?

I have implemented an event listener in my JS file following a successful AJAX request: var pageButtonsParentElement = document.getElementById("page-buttons"); pageButtonsParentElement.addEventListener('click', event => { ...