It appears that the zoom level is not being used and the map always shows fully zoomed in..

Comments

Brandonian’s picture

Status: Active » Fixed

Thanks for the bug report, @wipeout_dude. Some of this is a legitimate bug/regression from previous versions.

I've adjusted the zoom level given by the map to have the requested zoom level if the map is displaying a single point. Otherwise, the zoom level is determined by Google Map's fitBounds function, which will zoom in to fit the feature(s) being displayed.

http://drupalcode.org/project/geofield.git/commit/9f1a2ed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

ceepeebee’s picture

Title: Zoom level not working.. » Unexpected fitBounds() leads to a fully zoomed in map
Component: GeoFied Map » Code
Issue summary: View changes
Status: Closed (fixed) » Needs review

I reopen this issue due to another unexpected behaviour related to the problem above and therefore renaming the title. (I hope you don't mind)

I just experienced a fully zoomed in map, as well. In my case the error actually lay with me due to accidently passing the same geodata twice (problem with addressfiel module saving default country as a second record) - never mind.
Anyways, this showed me that I'd be running into the same problem lateron, when displaying geodata of two (or more) datasets when they are totaly equal.

Use case (I will use the term address instead of geocode):

1. let 'geofield map' output the addresses of a group of people.

2. If the group consists of only one person: no problem, only one marker, resetZoom = false; (applied by the patch mentioned above)

3. If there is more than one address, you have to distinguish:
a) one or more addresses differ: (almost)** no problem, resetZoom = yes; the map will be zoomed accordingly to fitBounds
b) BUT if by chance all the addresses are identical, then you get the same behaviour as before: a fully zoomed in map... that is unexpected.
It may seem to be not very likely to have the same addresses (geocodes) for all sets passed,
but just consider a student hall of residence.

My fix for geofield_map.js: (just using php for syntax highlighting

var resetZoom = false;   // this is the expected standard behaviour; 
                    // use the default zoom unless there is need to do otherwise
...
...
// lines 74 ff
if (features.setMap) {
            placeFeature(features, map, range);
            
           // Don't move the default zoom if we're only displaying one point.
           // we don't need that, because this should be the standard behavior
           /*
            if (features.getPosition) {
              resetZoom = false;
            }
            */
          } else {
            for (var i = 0; i < features.length; i++) {
              // Only reset zoom if there are __different__ points to display
              // check for difference and on success resetZoom
              // otherwise do nothing and
              // keep the previous current setting (false/true)
              // --> once true it stays that way 
              if (!resetZoom && features[i+1] && features[i].getPosition() != features[i+1].getPosition()) {
                  resetZoom = true;
              }
              
              if (features[i].setMap) {
                placeFeature(features[i], map, range);
              } else {
                for (var j in features[i]) {
                  if (features[i][j].setMap) {
                    placeFeature(features[i][j], map, range);
                  }
                }
              }
            }
          }

Questions arising:
1. what if there are any use cases which would rely on a zoom reset even if all the displayed geocodes fall into one singular spot (= automatic zoom in). I couldn't think of any, but then you could just use standard zoom level 0.

2. what do you consider to be two _different_ locations? I guess if you set a default zoom level you want to have at least this zoom level. Only if you need to zoom out in order to see other markers outside the standard view, you would need a map.fitBounds(range). Zooming in to fit bounds leads to the unexpected behaviour described above. But that's just my opinion.

3. Similarily, what do you do, if two points are not identical but lie just next to each other? e.g. two neighbours. If you set a standard zoom level of, say 10, in many cases it would not be expected to have an automatic zoom reset. (see above)

Cutting it short, maybe an option to toggle automatic zoomreset (maybe even seperate for zooming in and zooming out) should be implemented to avoid predefining the needs out there.

ceepeebee’s picture

Version: 7.x-1.x-dev » 7.x-2.1
ceepeebee’s picture

Sorry, the for-loop is incorrect in the previous post. It should read as follows or similar.
I am going to correct it.

for (var i=0; i < features.length; i++)
dariogcode’s picture

Hi @ceepeebee I applied your changes and it works well in some circunstances, but in other not, I have multiples points with different lat/lang, but still have wrong map zoom or fit bounds.

Anyone having same issue?

dariogcode’s picture

ok, for everybody trying to fix this. I fixed it (for now) using the following code:


if (resetZoom) {
	map.fitBounds(range);
	var listener = google.maps.event.addListener(map, "idle", function() { 
	  if (map.getZoom() > myOptions.zoom) map.setZoom(myOptions.zoom); 
	  google.maps.event.removeListener(listener); 
	});
} else {
	map.setCenter(range.getCenter());
}

Because the problem is when the fitbounds fully zoom the map, if the current zoom is more than our pre defined zoom then we need to set it again. Thanks to http://stackoverflow.com/a/4065006