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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:45:29+00:00 2026-05-14T15:45:29+00:00

using javascript google-maps api I currently have it setup to remove a maker I

  • 0

using javascript google-maps api

I currently have it setup to remove a maker I set it up when I am adding a location like so

  function addLocation(map,location) {
      var point = new GLatLng(location.lat, location.lon);
      var marker = new GMarker(point);
      map.addOverlay(marker);
      bounds.extend(marker.getPoint());

      $('<a href="#" class="closebutton">').click(function(e){
        e.preventDefault();
        $(this).parent().remove();
        map.removeOverlay(marker);
        map.closeInfoWindow();
    }).prependTo($('<li>'+location.label+'</li>').click(function() {
            showMessage(marker, location.label,map);    
      }).appendTo("#list"));
      GEvent.addListener(marker, "click", function() {
        showMessage(marker, location.label,map);
      });
  }

then I have a function that sets the zoom level

 function zoomToBounds(map) {
     map.setCenter(bounds.getCenter());
     map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
 }

this is called after my addLocations function and does what I want it do and sets the zoom level so I can see all the makers.

Now if I put a call to zoomToBounds right after

  map.removeOverlay(marker);

then it doesn’t move it just stays at the same zoom level

so what I want to know is if there is a way for me to set the zoom level after I remove a marker ??

  • 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-05-14T15:45:29+00:00Added an answer on May 14, 2026 at 3:45 pm

    Hey there – this is definitely something you can accomplish using Google Maps API.

    One important thing that you need to make sure you do is update the GLatLngBounds object before attempting to have the GMap2 object recalculate it’s position and zoom level.

    To do this, I would suggest keeping some sort of data store of all the points the GMarkers are using.

    Using GEvent Listeners you can also tie the zoomToBounds function to an event, when a GMarker is removed.

    Here is a code snippet of what I am talking about:

    var bounds = new GLatLngBounds();
    var points = {};
    
    function createMarker(location)
    {
         /*Create Our Marker*/
         var point = new GLatLng(location.lat,location.lon);
         var marker = new GMarker(point);
    
         /*Add an additional identifier to the Marker*/
         marker.myMarkerName = 'uniqueNameToIDMarkerPointLater';
    
         /*Store the point used by this Marker in the points object*/
         points[marker.myMarkerName] = point;
    
         /*Create an event that triggers after the marker is removed to call zoomToBounds*/
         GEvent.addListener(marker,"remove",function()
         {
              /*Passes the marker's ID to zoomToBounds*/
              zoomToBounds(this.myMarkerName);    
         };
    
         /*Add the new point to the existing bounds calculation*/
         bounds.extend(point);      
    
         /*Draws the Marker on the Map*/     
         map.addOverlay(marker);                  
    }
    
    function zoomToBounds(name)
    {
         /*Remove the Point from the Point Data Store*/
         points[name]=null;
    
         /*Create a new Bounds object*/
         bounds = new GLatLngBounds();
    
         /*Iterate through all our points and build our new GLatLngBounds object*/
         for (var point in points)
         {
              if (points[point]!=null)
              {
                   bounds.extend(points[point]);
              }
         }
    
         /*Calculate the Position and Zoom of the Map*/
         map.setCenter(bounds.getCenter());
         map.setZoom(map.getBoundsZoomLevel(bounds)-1);
    }
    

    The GLatLngBounds object does not store all of the points that it used to calculate it’s maximum and minimum bounds – so a new object needs to be created to redefine the bounds of the rectangle.

    I also created a functioning example of this located
    here.

    Feel free to use the source code to whatever you need – let me know how you make out with it, or if you have any other questions!

    Here is the code without any comments:

    var bounds = new GLatLngBounds();
    var points = {};
    
    function createMarker(location)
    {
         var point = new GLatLng(location.lat,location.lon);
         var marker = new GMarker(point);
         marker.myMarkerName = 'uniqueNameToIDMarkerPointLater';
         points[marker.myMarkerName] = point;
         GEvent.addListener(marker,"remove",function()
         {
              zoomToBounds(this.myMarkerName);    
         };
         bounds.extend(point);        
         map.addOverlay(marker);                  
    }
    
    function zoomToBounds(name)
    {
         points[name]=null;
         bounds = new GLatLngBounds();
         for (var point in points)
         {
              if (points[point]!=null)
              {
                   bounds.extend(points[point]);
              }
         }
         map.setCenter(bounds.getCenter());
         map.setZoom(map.getBoundsZoomLevel(bounds)-1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After the data of jqGrid are filled (for example infide… May 15, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer The problem was entirely something else.. Pylons always caches templates,… May 15, 2026 at 2:47 pm
  • Editorial Team
    Editorial Team added an answer Apparently this is standard behaviour! odd... May 15, 2026 at 2:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.