Create a dynamic and adaptable Google Maps experience without the need for an iframe

For instance, if you are using an embedded Google Map.

You can ensure that your Google Map is responsive with the following code:

  • Using iframe (simple iframe)
  • <div class="ggmap">
    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d212270.5451230493!2d-84.42060395!3d33.7677129!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x88f5045d6993098d%3A0x66fede2f990b630b!2sAtlanta%2C+GA!5e0!3m2!1sen!2sus!4v1396981185525" width="600" height="450" frameborder="0" style="border:0"></iframe>
    </div>
    
    .ggmap {
      position: relative;
      padding-bottom: 56.25%;
      padding-top: 30px;
      height: 0;
      overflow: hidden;
    }
    
    .ggmap iframe,
    .ggmapr object,
    .ggmap embed {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
    

    However, I prefer to use Google Maps via JavaScript like the example below.

  • No iframe (simple no-iframe)
  •  <div id="map_canvas" style="width:600px; height:450px"></div>
    
    function writeMap(latitude, longitude) {
      var latlng = new google.maps.LatLng(latitude, longitude);
      var opts = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map_canvas"), opts);
    }
    
    $(document).ready( function(){
            writeMap(latitude, longitude);       
    });
    

    In this scenario, how can I make the Google Map responsive?

    Answer №1

    Ensure that the container has defined height and width percentages to properly display content.

    var latitude = 42.0;
    var longitude = -72.0;
    function createMap(latitude,longitude) {
      var latlng = new google.maps.LatLng(latitude,longitude);
      var options = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map_canvas"), options);
    }
    
    $(document).ready( function(){
            createMap(latitude,longitude);       
    });
    body, html, #map_canvas {
      height: 100%;
      width: 100%;
      }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map_canvas" ></div>

    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

    Navigating through nested <ul> elements using Selenium and C#

    Currently, I am attempting to loop through the <li> items within a <ul> object using Selenium with C#. As a newcomer to this, I am exploring ways to simplify the coding process for my testing project. Below is a snippet of the HTML code that I ...

    Is there a way to shift this div to the bottom and incorporate some quirky pop-up squares?

    I am struggling to figure out how to move this div to the bottom. Additionally, I want to create temporary pop-up squares (simple divs) in random directions that will disappear after 50ms. My attempt to float this box to the bottom did not yield the desir ...

    The Javascript document refuses to load

    I am currently working on a website with the main file named index.html: <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>Title</title> </head> ...

    Guide on displaying a stationary image using flask and JavaScript

    Embedding an image in the HTML code is pretty straightforward: <img src="{{url_for('static', filename='#UserData/user/profile/profile_pic.jpg')}} " width='200' height='200' /> It works flawlessly wit ...

    Exploring the possibilities of extracting HTML content from custom tags in Angular JS

    I just finished developing a personalized directive. <foo> </foo> Is it possible to retrieve the HTML content enclosed within these tags? Take this example: <foo> <div> .... </div> </foo> My goal is to target and man ...

    What is the best way to eliminate the dotted border from clickable images?

    The client has expressed their dislike for the dotted border, but I believe it is important to keep it for accessibility reasons. However, I suppose I will have to remove it since the client requested it. How should I proceed? ...

    Using PHP to redirect upon clicking a link

    My issue involves directing users to another page or form and saving the button they click as a value when selecting a CRN (course number). <table class="table table-bordered table-hover table-striped"> <thead> ...

    Is it possible to detect inline elements that have been wrapped?

    I am facing a challenge with displaying an indefinite amount of in-line elements. Depending on the width of the browser, some elements may shift to a new line. I am curious to know if it is possible to identify and isolate these rows of elements, or if the ...

    "Implementing a Callback Function when Jquery Countdown Reaches

    Implementing this plugin will result in a live countdown displayed on the webpage. I recently reviewed the on.finish callback documentation found on the Official website. The main objective is to hide the span#limit once the timer completes its countdown ...

    If the width is below 767, the previous code will not be effective in jQuery

    Challenge: How can I ensure that the code within the if statement only executes when the screen width exceeds 767px, and does not work if it is less than 767px? Using jQuery: $(document).ready(function() { $(window).resize(function() { if ($( ...

    Upload file to database and move to new location

    In order to gain a clearer understanding, I have a question regarding inserting data from a form. Let's say I have the following form: <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <inp ...

    Is there a way to capture and print an entire DOM element with the help of angular-screenshot?

    I attempted to use the example code from this webpage: The challenge I faced was that they utilize an entire controller for it, whereas I have my controllers separated by views. Here is how I integrate them into my "main" js: .state("main.pallet", { ...

    How can I prevent the same JavaScript from loading twice in PHP, JavaScript, and HTML when using `<script>'?

    Is there a PHP equivalent of require_once or include_once for JavaScript within the <script> tag? While I understand that <script> is part of HTML, I'm curious if such functionality exists in either PHP or HTML. I am looking to avoid load ...

    When utilizing CKEDITOR, the default TEXTAREA is obscured, and CKEDITOR does not appear

    I am trying to incorporate CKEDITOR into my project. I have added the ckeditor script in the footer and replaced all instances of it. <script src="<?= site_url('theme/black/assets/plugins/ckeditor/ckeditor.min.js') ?>" type="text/javasc ...

    "Optimizing websites for Retina displays through pixel ratios and media queries

    Is there a way to make my max-width media queries effective for retina displays? I encountered an issue when testing my website on a macbook air with a pixel ratio of 2, causing my media queries to trigger at incorrect screen widths. For reference, here i ...

    Is there a way to modify the automatic zoom feature in Bootstrap 4 Carousel that adjusts the image to fit a certain section?

    Is it possible to change the specific area that is zoomed into in the image? While having the correct resolution would be ideal, I am unsure of the optimal resolution for the carousel. To see a demonstration, click on this link: https://jsfiddle.net/rmbe ...

    PHP Form Functions Properly on Firefox, but Encounters Issues on IE and Chrome

    I'm having an issue with a form I created to send to my email. The form works fine in Firefox, but it's not functioning properly in Chrome or Internet Explorer. Any assistance would be greatly appreciated! EDIT The problem is that the php code i ...

    Right-align SELECT-OPTIONS text

    Here are the screenshots of the form I'm currently working on. I am aiming to create a select box in the form where the text within the options is aligned to the right. After an option is selected, the chosen text should be displayed as illustrated i ...

    Using HTML5 canvas to draw circles with additional metadata stored in variables

    I'm designing a seating chart that will indicate spots with circles and show a tooltip on mouseover with the person's first name, last name, and possibly other details. I plan to use Kinetic.JS for region/tooltip support based on the tutorials I& ...

    Utilizing CSS :hover and :active states for internal elements

    I am working on creating a user-friendly sortable list using jQueryUI. When a user hovers over an item, I want to display options such as edit and delete. So far, this functionality is working well. However, the challenge I'm facing is making these o ...