How to Resize a Google Map After Loading with JavaScript?

Linda Hamilton
Release: 2024-11-04 09:10:30
Original
485 people have browsed it

How to Resize a Google Map After Loading with JavaScript?

Resizing a Google Map after Loading with JavaScript

Resizing a Google Map to fit a larger container after it has loaded can be achieved with a simple trigger. In Google Maps v3, the resize event is triggered differently:

google.maps.event.trigger(map, "resize");
Copy after login

Refer to the Google Maps API documentation for more information on the resize event.

Update:

For a practical example, consider the following code snippet using jQuery:

$(function() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644)
  };
  var map = new google.maps.Map($("#map-canvas")[0], mapOptions);

  // Listen for window resize event and trigger map resize
  $(window).resize(function() {
    google.maps.event.trigger(map, "resize");
  });
});
Copy after login

The above is the detailed content of How to Resize a Google Map After Loading 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