Jump to content
TNG Community
Sign in to follow this  
XerxX

Google Map: No auto zoom-in possible?

Recommended Posts

XerxX

Hi All,

Abstract: I want to prevent the "automatic zoom-in" that happens to the person maps when the place-markers are close together.

Basis: All Swedish places at my site are geocoded with a zoom level of 10.
When there are more than one marker and they are not close, the zoom level is lower to show all markers.

Problem: When the place-markers are close together there is an "automatic zoom-in" performed like in this example:
https://xerxx.se/getperson.php?personID=I435298&tree=tredNr2

You have to zoom out to see where in Sweden we are.

As you see, I use the "Person map" mod and you can close it. When you re-open the map zooms out, to level 10.
Reload the page and the map is zoomed in again.
The effect is the same for both "Yes" and "No" in Admin >> Setup >> Map Settings >> Reset location markers on unhide.

I can understand the thinking behind the behaviour (to separate the markers) but for me it's better to keep the level of 10 as that will guide you to where in the country the place is located. If it's interesting for you, you can zoom in manually.

This is probably not caused by the mod, as I see the same zoomed-in level at my localhost copy site when I uninstall the mod.

Is there any way to prevent this zoom-in?

Thanks in advance for any help,

Erik

Share this post


Link to post
Share on other sites
XerxX

Problem Solved!

 

I searched the Net and found this page:

https://stackoverflow.com/questions/2437683/google-maps-api-v3-can-i-setzoom-after-fitbounds

All replies boiled down to change "bounds_changed" to "zoom_changed" in this code in googlemaplib.php

            echo "   map.fitBounds(bounds);\n";
            echo "   google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {\n";
            echo "       if (map.getZoom() > $zoom) {\n";
            echo "           map.setZoom($zoom);\n";
            echo "       }\n";
            echo "   });\n";

I have mailed Darrin about this. I don't know if he will change it.

 

Best regards,

Erik

Share this post


Link to post
Share on other sites
XerxX

Darrin has confirmed the error. It's going to be changed in v12.

He also pointed out that the Heatmap has the same problem.

 

And if you, like me, have your own custom maps, they maybe need to be updated.

 

/Erik

Share this post


Link to post
Share on other sites
XerxX

NOTE!

If you follow the link in my OP the map is zoomed out because I've made the change to the code.

But if you close the map and re-open it, it is zoomed in!

 

I think it's connected to the "Mobile ind page person map" v11.0.0.1 that I use:

https://tng.lythgoes.net/wiki/index.php?title=Person_Map

It may be because the listener is added once and we can close&open the map: When the map is re-opened the listener is gone...

Maybe you understand this better than I do?

 

Thanks in advance for your input,

Erik

Share this post


Link to post
Share on other sites
Ken Roy
7 minutes ago, XerxX said:

It may be because the listener is added once and we can close&open the map: When the map is re-opened the listener is gone...

Erik,

I am not sure what you are saying in the above sentence.

Share this post


Link to post
Share on other sites
XerxX

Hi Ken,

 

What I mean is that the code in googlemaplib.php:

On 2018-02-18 at 3:09 PM, XerxX said:

google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {....

adds the listener (for "zoom_change" after modif.) once, and so - if I understand this - will be automatically deleted after a call.

That call could be when the page is loaded (result: The map is zoomed-out).

So, my thinking is that the listener isn't there when the map is re-opened and so the code is not executed.

 

But as I said: I don't understand this "listener thing" very well, so I understand if you don't understand me...

Thanks,

Erik

Share this post


Link to post
Share on other sites
Ken Roy

I have no clue on the listener either.   But looking at the Stack Overflow link you provided it looks like the voted answer uses

map.fitBounds(bounds);
var listener = google.maps.event.addListener(map, "idle", function() { 
  if (map.getZoom() > 16) map.setZoom(16); 
  google.maps.event.removeListener(listener); 
});

Did Darrin indicate what his fix for TNG v12 was going to be?

Share this post


Link to post
Share on other sites
theKiwi

I don't think that "addListenerOnce" is a recognised Google call now - it's certainly not mentioned on the the Google pages that describe the "addListener" function.

Roger

Share this post


Link to post
Share on other sites
XerxX

Ken,

If I don't misunderstand Darrin is going to replace "bounds_changed" by "zoom_changed".

If you use "idle" instead the map will zoom to the default level in front of your eyes. Not nice. Some replies indicate this.

And if you use "addListener" you have to use removeListener to remove it. That's the normal case.

(Add listener Once will remove it automatically after the call - AFAIU)

I'm talking about the case where the map can be closed and re-opened.

Maybe the mod should use "addListener" and NO "remove"?

 

Roger,

Why don't you think it's recognized when it works?

And it's certainly in here: https://developers.google.com/maps/documentation/javascript/reference?csw=1#event (maybe scroll down to "event namespace")

 

Best regards,

Erik

Share this post


Link to post
Share on other sites
XerxX

Hi All, again ;)

 

I am not good at explaining things - I know that. People misunderstand all the time. Also in this thread.

But I thought that if I show you exactly how I noticed this change in behaviour, because it's so obvious, you would understand better.

 

This is a test site at my localhost with an index page that I created from template 15.

The site is a test for a "three parishes study" (50 places) and when you open the index page - not logged in - there is this map:

toKen1.jpg

It shows "Our 50 villages / residences..." and is not modified by the Person Map mod.

"Logga in" in the menu bar is the "Log in" link (the site is Swedish only). When you log in, this is the map and menu bar:

toKen2.jpg

It has been like this since the summer/early autumn, when I created it.

 

Now, all of a sudden like a week ago, the NOT logged in visitor had the SAME zoom level: (Check the menu bar...)

toKen3.jpg

So I was flabberghasted, ripped my code apart and couldn't find what was wrong.

I spent two full days convinced that the error was in my code.

So I had to ask for help.

I knew about that lady, that I linked to in the OP.

So to be able to show an example, I created the addresses as separate places - the Residence events all simply pointed to the town.

 

When later I got Darrin's reply/confirmation, I thought I'd alert developers to at least test their mods - because of the behaviour of the Person Map mod.

So I did.

 

I have also tried to keep the Person Map mod zoomed out after Closing/Opening the map (without reloading the page) but can't.

But then again; I'm not sure how this "listener thing" really works...

 

Best regards,

Erik

Share this post


Link to post
Share on other sites
theKiwi
20 hours ago, XerxX said:

Why don't you think it's recognized when it works?

And it's certainly in here: https://developers.google.com/maps/documentation/javascript/reference?csw=1#event (maybe scroll down to "event namespace")

 

I did a Google search for "addlisteneronce" and of the several Google maps pages returned, none of them included that string - they included addListener, but not addListenerOnce.

I did not find the page you link above.

Roger

Share this post


Link to post
Share on other sites
XerxX

Hi Roger,

 

I click the link and get this page:

toRoger.jpg

It's there and it works.

It's the behaviour of the parameter "bounds_changed" that has changed. So it's better to use "zoom_changed" now.

(You can also use "idle" if you want a "live zoom" ;) )

 

Erik

Share this post


Link to post
Share on other sites
Ken Roy

Erik,

From everything I am seeing in the Google Maps API documentation, automatic zoom appears to be the the behavior of their map server.  I doubt the changing the bounds_changed to zoom-changed will resolve this issue.

Several requests have been submitted for options to disable automatic zoom and answers all appear to indicate that it is not possible.

Share this post


Link to post
Share on other sites
XerxX

Ken, it resolved it for me as I showed in my nest-to-last post.

 

We can do like this if you want:

At 23:00 GMT (some 1,5 hours from now) I revert the code at my site to "bounds_changed" and you can compare the result at this person (the same as in my OP) for one hour.

https://xerxx.se/getperson.php?personID=I435298&tree=tredNr2

Is that okay for you?

 

Erik

Share this post


Link to post
Share on other sites
Ken Roy

If it resolved it then why are you raising issues with the Person Map mod

Share this post


Link to post
Share on other sites
XerxX

Beacause it didn't resolve it completely if one uses that mod.

If you open the link now and then Close/Open the map (without reloading the page) it is heavily zoomed in.

It stayed zoomed out before the behaviour of the "bounds_changed" changed.

 

It's completely resolved if one doesn't use the mod.

That's what I say.

Share this post


Link to post
Share on other sites
Ken Roy

Except the Person Map mod does not make any changes to the googlemaplib.php.  So if a change is needed there it needs to be made in the TNG code. 

All the Person Map mod does is add new options to the Map Config.  It does not change the behavior of the TNG Google Map API code.  If that needs to be changed, then Darrin needs to make the change to the googlemaplib.php

Share this post


Link to post
Share on other sites
Ken Roy

In fact, I would really like to see Darrin make Bart's mod part of TNG, including the mobile version, since Bart is no longer active on TNG

Share this post


Link to post
Share on other sites
XerxX

Ken, Why don't you just click the link, look at the map, close it and open it without reloading the page?

Then tell me if that zoomed-in behaviour is something you recognize.

Share this post


Link to post
Share on other sites
XerxX
18 minutes ago, Ken Roy said:

Except the Person Map mod does not make any changes to the googlemaplib.php.  So if a change is needed there it needs to be made in the TNG code. 

All the Person Map mod does is add new options to the Map Config.  It does not change the behavior of the TNG Google Map API code.  If that needs to be changed, then Darrin needs to make the change to the googlemaplib.php

I think the mod can do it in googlemaplib.php using

if ($textpart == "getperson") {

    echo ".....

} else {

    // Darrins original code

}

 

The problem is what to use to stay zoomed-out...

/Erik

Share this post


Link to post
Share on other sites
Ken Roy

I am sure you could create the same problem with other TNG maps, like the cemetery maps.  So I see this as a TNG problem and not a mod problem.

Share this post


Link to post
Share on other sites
XerxX

No other map can be closed.

So I am just as sure that this zoomed-in behaviour is exclusive for the Person Map mod.

/Erik

Share this post


Link to post
Share on other sites
Ken Roy

I don't mean to be giving you a hard time, but I have no clue on how to fix this.  I am not a javascript programmer nor understand the Google Map API.  If I did, I would have reworked a mod that I used a long time ago that would show the boundary of regions.

Share this post


Link to post
Share on other sites
XerxX

All is fine Ken.

As I do understand some JS I'm actually just now trying to see if something can be done.

I think there is something about closing the map, and that makes JS revert or drop some values... I think.

That's why I tried before to use the "loadListener()" function without the required "removeListener()" but that didn't work.

If I find something I'll let you know. But don't hold your breath.

 

BTW: I would also like the Person Map to be TNG standard.

Erik

Share this post


Link to post
Share on other sites
Ken Roy

I have asked Darrin multiple times to include Bart's mod.  If other users don't ask Darrin to include the mod into TNG, it is not likely to happen

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×