Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:41:06+00:00 2026-06-18T12:41:06+00:00

I was trying to simplify the way I add Google maps to my pages.

  • 0

I was trying to simplify the way I add Google maps to my pages.

I’m using this markup for each map:

<div class="map" data-coordinates="-34.397, 150.644">
    <div class="canvas" id="map_canvas"></div>
</div>

and this jquery code:

jQuery(function($) {

    var maps = $('.map');

    maps.each(function() {
        var $this = $(this),
            mapId = $this.find('.canvas').attr('id'),
            coordinates= $this.data('coordinates');

        // set map options
        var mapOptions = {
            center: new google.maps.LatLng(coordinates),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // create the map
        var map = new google.maps.Map(document.getElementById(mapId), mapOptions);
    });
});

this is a recreation of the tutorial on the google maps website.

when I do it like the tutorial it works fine, but when I use my code above I get the map controls with a grey background, I also tried jQuery(window).load(); with the same result, and the issue seems to be from each() because when I create the map without it, it works fine.

this is the code that works:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:41:08+00:00Added an answer on June 18, 2026 at 12:41 pm

    There’s a problem with coordonates.

    google.maps.LatLng() accepts Lat and Lng as separate params, not a composite string.

    Try :

    jQuery(function($) {
        $('.map').each(function() {
            var $this = $(this),
                canvas = $this.find('.canvas').get(0),
                coordinates = $this.data('coordinates').split(/,\s?/);
    
            // set map options
            var mapOptions = {
                center: new google.maps.LatLng(Number(coordinates[0]), Number(coordinates[1])),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
    
            // create the map
            var map = new google.maps.Map(canvas, mapOptions);
        });
    });
    

    You might need to test that regexp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to simplify a Card class, and am wondering if there's any way
Hi I'm trying to simplify this code but can't think which way to do
I am trying to figure out how to simplify this piece of code. The
I'm trying to simplify a view from spree for a tablet, using mobile-fu ,
I'm trying to make z3 (I'm using z3py) to simplify some formulas for me
I'm trying to simplify a form (using ASP.NET MVC but I guess that might
I'm still trying to understand what is the correct way of implementing MVC. This
I'm trying to use Android Application class (MyApplication.java) for store data in some ArrayList
Is there any way to simplify this code? Or any way to create another
I am trying to come up with the best way to implement SQL Data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.