Integrating dual Google Maps onto a single HTML page

I'm facing an issue with implementing two Google maps on a single page where the second map seems to be malfunctioning.

Below is the code I am currently using:


<style>
#map-london {
    width: 500px;
    height: 400px;
}
#map-belgium {
    width: 500px;
    height: 400px;
}
</style>

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<script>
    function initialize() {
      var myLatlng = new google.maps.LatLng(51.518865,-0.133108);
      var mapOptions = {
        zoom: 17,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById('map-london'), mapOptions);

      var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: 'Incopro London'
      });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

<script>
    function initializeB() {
      var myLatlngBel = new google.maps.LatLng(50.802138,-1.07839);
      var mapOptionsBel = {
        zoom: 17,
        center: myLatlngBel,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var mapB = new google.maps.Map(document.getElementById('map-belgium'), mapOptionsBel);

      var markerB = new google.maps.Marker({
          position: myLatlngBel,
          map: mapB,
          title: 'Incopro Belgium'
      });
    }

    google.maps.event.addDomListener(window, 'load', initializeB);
</script>

To display the maps, I have utilized two div elements:

<div id="map-london"></div>
<div id="map-belgium"></div>

If anyone has any insights on how this can be resolved, please provide your input.

Thank you in advance.

Answer №1

Consider consolidating all the code into a single initialization function:

<script>
    function setupMaps() {
      var londonCoords = new google.maps.LatLng(51.518865,-0.133108);
      var belgiumCoords = new google.maps.LatLng(50.802138,-1.07839);

      var mapOptionsLondon = {
        zoom: 17,
        center: londonCoords,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var mapOptionsBelgium = {
        zoom: 17,
        center: belgiumCoords,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }

      var londonMap = new google.maps.Map(document.getElementById('map-london'), mapOptionsLondon);
      var belgiumMap = new google.maps.Map(document.getElementById('map-belgium'), mapOptionsBelgium);

      var londonMarker = new google.maps.Marker({
          position: londonCoords,
          map: londonMap,
          title: 'Incopro London'
      });
      var belgiumMarker = new google.maps.Marker({
          position: belgiumCoords,
          map: belgiumMap,
          title: 'Incopro Belgium'
      });
    }    
    google.maps.event.addDomListener(window, 'load', setupMaps);
</script>

Answer №2

Check out the JS Fiddle link below for a demonstration:

http://jsfiddle.net/89hv75h1/1/

    <!-- Sample Map -->
       <div id="map-canvas" style="width:100%; height:333px"></div>
         <br/><hr/>
       <!-- Another Map -->
       <div id="map-canvas_2" style="width:100%; height:333px"></div>
<script>
     function initialize() {
          var myLatlng = new google.maps.LatLng(25.18694, 55.2609596);
          var myLatlngBel = new google.maps.LatLng(25.18694, 55.2609596);

          var mapOptions = {
            zoom: 17,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }

          var mapOptionsBel = {
            zoom: 17,
            center: myLatlngBel,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }

          var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
          var mapB = new google.maps.Map(document.getElementById('map-canvas_2'), mapOptionsBel);

          marker = new google.maps.Marker({
        map:map,
        draggable:false,
        animation: google.maps.Animation.BOUNCE,
        position: myLatlng,
          icon: "http://oi62.tinypic.com/6fxyx5.jpg"
      });

          markerB = new google.maps.Marker({
            map:mapB,
            draggable:false,
            animation: google.maps.Animation.BOUNCE,
            position: myLatlngBel,
             icon: "http://oi62.tinypic.com/6fxyx5.jpg"
          });
            marker.setAnimation(google.maps.Animation.BOUNCE);
            markerB.setAnimation(google.mapB.Animation.BOUNCE);
        }   
        google.maps.event.addDomListener(window, 'load', initialize);


    $(document).ready(function() {

            initialize();

    });
</script>

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

Tips on showcasing information with ng for in Ionic 2

I am currently working on the thank you page for my project. When the success call is made, it will display both ORDER DETAILS and Delivery Details. However, in the event of a failure call, only the ORDER DETAILS are displayed. While the success call is f ...

Failure to Trigger AJAX Success Event

I am facing an issue while trying to retrieve a JSON string using an ajax call with JQuery. Despite getting a code 200 and success from the complete method, the success method itself is not triggering. function initialize() { $.ajax( { type ...

How can data be transferred between web pages using jQuery or JavaScript?

Imagine having two different pages on a Classified car website: The first page is the search page, which displays a list of all cars. Check out a sample search page here The second page is the details page, where you can find specific information about a ...

Utilizing Vue refs to set focus on a specific element target

Clicking on "span class="before-click" should hide it, and display input class="after-click" instead. The displayed input must be in focus! However, when trying to use $refs.afterClick to access the DOM and apply .focus(), an unexpected error stati ...

The sequence of CSS and deferred JavaScript execution in web development

Consider this scenario: you have a webpage with a common structure in the <head>: <link rel="stylesheet" href="styles.css"> // large CSS bundle <script defer src="main.js"></script> // small JS bundle with defer attribute There is ...

Unable to activate click function in Jquery

Here is a basic HTML page snippet: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script> <script> $(document).ready(function () { $('#test').click(); }); < ...

Incorporating a variety of classes by utilizing loops

Looking to add a class to specific li elements - the 1st, 4th, and 7th, the 2nd, 5th, and 8th, and the 3rd, 6th, and 9th. Is this possible? Is there a way to achieve this? ...

Another option instead of sending back a JavaScript tag in the Ajax response

After making an ajax request in my code, I receive some HTML that contains specific elements requiring custom event handling. For example: <div> <!--some html--> <button id='specialbutton'></button> </div> <scr ...

What is causing the alt or title tags to not function properly in emails?

I am currently facing an issue with e-mail templates that include images. The problem arises from the fact that these images need to be downloaded before they can be displayed in the email. To work around this, I always use ALT and TITLE tags to provide de ...

Adding plain HTML using jQuery can be done using the `.after()` and `.before()` methods

I have encountered a situation where I need to insert closing tags before an HTML element and then reopen it with the proper tags. The code snippet I am using is as follows: tag.before("</div></div>"); and then re-open it by adding proper tag ...

The background gently fades away, directing all attention to the popup form/element

After extensive searching, I have yet to find a straightforward solution that seems to be a standard practice on the web. My goal is to create a form that appears in the center of the screen when a button is clicked using CSS or jQuery. I would like the ...

How can we transfer functions between files in JavaScript when creating a service library?

There's a piece of code located in my identity service that I'm working with. export function sumbitLogin(username, password) { console.log(username, password); } I want to simplify the process of accessing services in my components without ...

Why is the jQuery change event only firing when the page loads?

I am experiencing an issue with a .js file. The change event is only triggering when the page loads, rather than when the selection changes as expected. $(document).ready(function(){ $("#dropdown").on("change keyup", colorizeSelect()).change(); }); f ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

Switch the style sheets after the content

How can I create a toggle effect on the :after property, switching between + and - each time the link is clicked? I've attempted to achieve this using JavaScript, CSS, and HTML, but the code only changes + to - and doesn't switch back when the l ...

The datepicker is refusing to update the date format

I've been attempting to adjust the date format for my datepicker, but it refuses to change. Below is the code I'm using: $(document).ready(function() { $('#dateselect').datepicker({ format: 'dd/mm/yyyy', o ...

What is the method for implementing a distinct background in dark mode while utilizing Material UI Themes?

I am facing an issue with changing the background color when in dark mode. Here is my initial code snippet... export const myThemeOptions: ThemeOptions = { palette: { mode: "light" as PaletteMode, primary: { main: ...

Daily loop countdown timer

Looking to implement a daily countdown timer that ends at 10am, I have the following code set up: setInterval(function time(){ var d = new Date(); var hours = 09 - d.getHours(); var min = 60 - d.getMinutes(); if((min + '').length == 1){ ...

JavaScript utilized to create a fully immersive full-screen webpage

I have been trying to implement a code for creating a full-screen website that works on all browsers. Unfortunately, my current code only seems to be functioning properly on Mozilla browser. When I try to view the site in Chrome and make it full screen, it ...

Setting the borderRadius for a web view on Android Maps V3: A complete guide

In my application, I am using Android Map V3 and have encountered a bug specific to the Kit-Kat version. The chromium kit throws up an error message - nativeOnDraw failed; clearing to background color, which prevents the map from being displayed. Despite ...