printGoogleMap callback example


Can you give some code examples to use callback function to set map options with printGoogleMap function.

For instance, I would like to activate the auto zoom, modify map options etc. For some reason the zoom of the map is not correct.

  • @param function $callback optional callback function to set map options.



  • acrylian Administrator, Developer
    edited July 2020

    You create a function that takes and modifies the $map object created within that function on line 387. The callback is then used in line 423.

    I have never used this as I favor if the openstreetmap plugin.

    It should IMHO work like this. $map must be used as a reference as far as I see:

    Create a function like

    function yourcallbackfunction(&$map) {
       // here modify the $map object as you wish

    Then use

    printGoogleMap(NULL, NULL, NULL, NULL, 'yourcallbackfunction');

    Easier way might be to use the actual GoogleMap class directly instead of printGoogleMaps) which is just a convenience wrapper of that anyway. There you can directly pass your configuration to it without this callback workaround. Usage example within that function itself.

  • I have tried but it does not seem to work, it all clears my pins:

    function mapcallback(&$map) {
    $map->zoom = 'auto';
    printGoogleMap(NULL, 'gmap', 'show', 'mapcallback');

    Do I need to return the $map object?

  • acrylian Administrator, Developer

    printGoogleMap(NULL, 'gmap', 'show', 'mapcallback');

    Try the correct parameters ;-) The callback is the 5th paramater ;-)

    Do I need to return the $map object?

    No, should not, the callback use does not use any return. That's why it is ipassed by reference in my example reason actually. Again, I never tried this actually. Perhpas try using the class directly, it is not that much more complicated.

  • Just curious:

    Is $map->zoom = 'auto'; what you are trying to do? Because that should already have been done in the default setup.

  • davidarnoult Member
    edited July 2020

    Thanks @acrylian and sorry for the beginner mistake ;-).

    When calling the callback function ($map->zoom = 'auto'), I have this error message that I don't understand:
    AVERTISSEMENT : Parameter 1 to mapcallback() expected to be a reference, value given dans server_path/zp-core/zp-extensions/GoogleMap.php à la ligne 423

    printGoogleMap called from include (album.php [133])

    If it is not working well, I will use the class directly as you advise...

    @sbillard I am trying to force the auto zoom feature as it does not work for me by default all the time. I would like also to change the height of the map too but I am not sure if it is possible using the $map object but javascript....

    Thanks guys for your support anyway ;-)

  • I must precise that when I set $map->zoom = 15; it works but with the same warning. Forcing to auto does not work as default behaviour does not work either...

  • acrylian Administrator, Developer
    edited August 2020

    I just took a look. Please try changing line 423 within printGoogleMap() where it reads

    call_user_func($callback, $map);


    call_user_func($callback, &$map);

    Or the callback must return $map like
    $map = call_user_func($callback, $map);
    instead. But the result will be the same.

    Since we call a function via function it probably requires an extra reference definition here.

    Forcing to auto does not work as default behaviour does not work either...

    Within the template function "auto" is actually hardcoded as mentioned above. If it does not work perhaps it works but for some reason not as expected?

  • fretzl Administrator, Developer

    I would like also to change the height of the map too...

    That can be done by adding a rule for #map_canvas in your CSS.

    1. When objects are passed as parameters for all practical purposes they are passed by reference, ( so the code presented by acrylian is redundant. The callback function will modify the actual map object.

    2. The real issue seems to be the interpretation of the $map->zoom="auto" setting. In reality, this is used to invoke fitting the map to the bounds of several geopoints. As noted above, zoom="auto" is set by default. But if there is only one getpoint it will be overridden and set to 13 because the fitting fails miserably. There is no possibility of automatically zooming to contain only one point. (Note also that if you examine the javascript code generated, the javascript zoom option will always be set to 13 if the map object zoom property was set to 'auto'. Automatic zooming only occurs if there are multiple geopoints. I do not know why 13 was selected, seems arbitrary, but it does work.)

  • acrylian Administrator, Developer

    the code presented by acrylian is redundant

    Indeed it is.

  • Thank you guys. I have found what I need!

    @acrylian I have choosen googlemap because openstreetmap plugin even if it was my choice at first place does not load map tiles correctly with bootstrap 4 js when map is displayed in a tab panel. It looks like js order loading issue I guess... but it is fine anyway with googlemap.

Sign In or Register to comment.