Say one has a small map, 300 x 300 px, and a single marker. The marker could be a geocoded street address or country taxonomy term. Different zoom levels would be required for a better user experience. One could set the average zoom in a map definition. However, what if one could set the zoom based on what was geocoded?

I've attached a work-in-progress for the addressfield type. It sets the zoom according to the fields that were completed. It might benefit from fine-tuning.

I noticed there's an unused accuracy column in geofield. Perhaps that could be of use in future.

CommentFileSizeAuthor
leaflet-autozoom.diff2.5 KBnetbek

Comments

rdeboer’s picture

Smart idea! I like it. Thanks for sharing.

pvhee’s picture

Status: Active » Needs work

We probably should add an option in the map definition to either set the map zoom level to 'auto' (and use your patch above, nice work), or to a specific zoom level.

denix’s picture

Nice idea, but manually setting the zoom still remain a priority.

pvhee’s picture

Manually setting the zoom is now possible using the "zoom" option in hook_leaflet_map_info.

It will then always use that zoom level, so for one marker, but also multiple marker maps you perfectly control the zoom level now.

denix’s picture

This is a great improvement, what is blocking for the release of a beta2 module? your module changed a lot since the beta1 version. great and nice work anyway.

pvhee’s picture

Unless comments from #2 are addressed, this won't enter in a new release. The zoom level definition from the hook is already in, and you can check out a dev release while a new release is being prepared.

gmclelland’s picture

I tried creating a custom module that implements this hook, but my map only shows a grey box with a pin in the middle. The map is a leaflet view.

<?php

/**
 * @file
 * API documentation for Administration menu.
 */

/**
 * Define one or map definitions to be used when rendering a map.
 * leaflet_map_get_info() will grab every defined map, and the returned
 * associative array is then passed to leaflet_render_map(), along with a
 * collection of features.
 *
 * The settings array maps to the settings available
 * to leaflet map object, http://leaflet.cloudmade.com/reference.html#map-properties
 *
 * Layers are the available base layers for the map and, if you enable the
 * layer control, can be toggled on the map.
 *
 * @return array
 */
function cmf_leaflet_leaflet_map_info() {
  return array(
    'OSM Mapnik - Zoom 14' => array(
      'label' => 'OSM Mapnik - Zoom 14',
      'description' => t('Leaflet default map.'),
      'settings' => array(
        'dragging' => TRUE,
        'touchZoom' => TRUE,
        'scrollWheelZoom' => TRUE,
        'doubleClickZoom' => TRUE,
        'zoomControl' => TRUE,
        'attributionControl' => TRUE,
        'trackResize' => TRUE,
        'fadeAnimation' => TRUE,
        'zoomAnimation' => TRUE,
        'closePopupOnClick' => TRUE,
        //'layerControl' => TRUE,
        'minZoom' => 10,
        'maxZoom' => 15,
        'zoom' => 14, // set the map zoom fixed to 15
      ),
      'layers' => array(
        'earth' => array(
          'urlTemplate' => 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
          'options' => array(
            'attribution' => 'OSM Mapnik'
          )
        ),
      ),
      // Uncomment the lines below to use a custom icon
      // 'icon' => array(
      //   'iconUrl'       => '/sites/default/files/icon.png',
      //   'iconSize'      => array('x' => '20', 'y' => '40'),
      //   'iconAnchor'    => array('x' => '20', 'y' => '40'),
      //   'popupAnchor'   => array('x' => '-8', 'y' => '-32'),
      //   'shadowUrl'     => '/sites/default/files/icon-shadow.png',
      //   'shadowSize'    => array('x' => '25', 'y' => '27'),
      //   'shadowAnchor'  => array('x' => '0', 'y' => '27'),
      // ),
    ),
  );
}

I'm able to select the map definition in the leaflet view. Any idea of what I'm doing wrong?

gmclelland’s picture

Well, it seems to work fine without Panels using the leaflet map as a formatter on a geofield. I'm not sure what is going on. My view was a content pane.

levelos’s picture

Issue summary: View changes
Status: Needs work » Closed (fixed)

This has been addressed in subsequent releases.

n20’s picture

Confirm using 0.75 works. But only fixed zoom levels seem to work. Using the option 'Use map defined setting' results in gray tiles.

n20’s picture

Mh, placing a second marker in some distance makes the map working with automatic zoom level. Only with a single marker and the resulting maximum zoom level the map gets grey. Using single marker and zooming out works for me.

n20’s picture

Mh, placing a second marker in some distance makes the map working with automatic zoom level. Only with a single marker and the resulting maximum zoom level the map gets grey. Using single marker and zooming out works for me.