When the geolocate behavior is enabled, but 'Center when located' (which is the bind option) is set to 'No', the map still centres on the user's current location.

The reason for this is that in the javascript file openlayers/plugins/behaviors/openlayers_behavior_geolocate.js, the value of options.bind is the string '0', rather than a boolean value. Later, in the library file libraries/openlayers/lib/OpenLayers/Control/Geolocate.js, in the geolocate() function, this.bind is used in an if statement. Because in JavaScript an non-empty string is always true, this.map.setCenter(center) is called. (JavaScript and PHP are different in this; in PHP, the string '0' would evaluate to false).

This means it's impossible to turn the 'Center when located' feature off.

For me, adding the following lines at the start of the anonymous function in openlayers_behavior_gelocate.js fixed the issue:

if (options.bind === '0') {
options.bind = false;
}

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Pol’s picture

Hi mooey,

Nice catch there, I would prefer doing the variable mapping directly in the PHP file. The lesser JS we have, the better it is.
Check the render() method of the geolocate behavior and try to adjust the variable casting in there.

I'll commit the changes as soon as I have your code.

Thank you very much!

mooey’s picture

Hi Pol

That makes sense.

I've attached a patch file. Because I don't know what the effect of changes $this->options would be, I've changed return $this->options to

$options = $this->options;
$options['bind'] = !!$options['bind'];
return $options;

Pol’s picture

Status: Active » Fixed

Good, committed !
Can you test and report back ?

Thanks!

Pol’s picture

mooey’s picture

Hi Pol

That works perfectly for me.

Cheers

Status: Fixed » Closed (fixed)

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