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 7772855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:58:37+00:00 2026-06-01T16:58:37+00:00

I have a map with multiple locations on it. The locations are determined at

  • 0

I have a map with multiple locations on it. The locations are determined at runtime depending on the page displayed (they are locations for our dealers, so they vary based on the dealer id. (The markers display fine, but now I want to center the map so that it is zoomed to include all of the markers and centered based on the markers displayed. How do I do this? Right now, the zoom and center are set manually. Here is my code so far:

<script type="text/javascript">
    function initialize() {
    var locations = [
        <?php 
            $loc = '';
            $i = 1;
            foreach ($coordinates as $c)  {
                $loc .= '["' . $c->company . '", ' . $c->latitude . ', ' . $c->longitude . '], ';
            $i++;
            }
            echo substr($loc, 0, -2); 
        ?>
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    }

    google.maps.event.addDomListener(window, 'load', initialize);
  </script>
  • 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-01T16:58:38+00:00Added an answer on June 1, 2026 at 4:58 pm

    Create google.maps.LatLngBounds object, which contains all markers’ positions, and then use fitBounds method of google.maps.Map object:

    var bounds = new google.maps.LatLngBounds();
    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });
      //Extends bounds to contain marker position
      bounds.extend( marker.getPosition() );
      .....
    }
    
    map.fitBounds( bounds );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have map with multiple different colors markers with some info window. Everything works
How should I map multiple columns with List in IBATIS ? I have a
Is it preferred to have one depot with multiple folders which map to different
I have successfully implemented code to plot multiple locations on google maps dynamically. Solution:
I have multiple classes that I need to map into 1 class: This is
I'm trying to have multiple values for profFName and profLName display on a map
I have a map with multiple markers, which I populate via an array. Each
I have and interface where a user can manage multiple locations for their business,
I have used below code for displaying multiple marker in the google map and
I have added multiple ovals on map view by using below code but I

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.