Home > Web Front-end > CSS Tutorial > How to Resize a Google Map Dynamically with JavaScript?

How to Resize a Google Map Dynamically with JavaScript?

Linda Hamilton
Release: 2024-11-04 18:12:02
Original
258 people have browsed it

How to Resize a Google Map Dynamically with JavaScript?

How to Resize a Google Map with JavaScript after Loading

When working with Google Maps, it's sometimes necessary to resize the map after it has already loaded. This can be achieved through JavaScript, but it requires some specific steps to ensure that the map tiles adjust accordingly.

To resize a Google Map in v3, follow this procedure:

  1. Resize the parent
    or container element that holds the Google Map using JavaScript or CSS.
  2. Trigger the "resize" event on the Google Map object.

Triggering the Resize Event

In Google Maps v3, the resize event needs to be triggered explicitly:

<code class="javascript">google.maps.event.trigger(map, "resize");</code>
Copy after login

where map is the Google Map object that needs resizing.

Example with jQuery

Here's an example using jQuery to handle window resizing and triggering the Google Map resize:

<code class="javascript">$(function() {
  var map = new google.maps.Map($("#map-canvas")[0], mapOptions);

  $(window).resize(function() {
    google.maps.event.trigger(map, "resize");
  });
});</code>
Copy after login

By following these steps, you can effectively resize a Google Map and ensure that the map tiles adjust and display properly within the new dimensions.

The above is the detailed content of How to Resize a Google Map Dynamically with JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template