hi,
My question is not directly about ol module, but I use it in other part.

I try by creating a module to generate a page with a simple map(drupal7 firefox or opera on debian squeeze).
I have no javascript errors, no conflict, but the page don't load the map.
why ? please see below the module code and please tell me where I am wrong :

<?php
// $Id$

/**
 * @file
 */

/**
 * Implements hook_menu().
 */
function tests_menu() {
  $items['tests'] = array(
    'title' => 'Tests',
    'description' => 'tests',
    'page callback' => 'tests_inserts',
    'access callback' => TRUE,
  );

  return $items;
}

/**
 * 
 */
function tests_inserts() {
	drupal_add_library('tests', 'openlayers');
	drupal_add_js("window.onload = init;var map;var center;function init(){var map = new OpenLayers.Map('container'); map.addLayer( new OpenLayers.Layer.OSM() );var center = new OpenLayers.LonLat(30000,5800000);map.setCenter(center, 6);alert('testinit2')}",'inline');
	$output = '';
	
	$output .= '<div style="width:100%; height:100%; border: 1px blue solid;">TESTS</div>';
	$output .= '<div id="container" style="border: 1px red solid;"></div>';

	return $output;

}

/**
 * Implements hook_library().
 */
function tests_library() {
  $libraries['openlayers'] = array(
    'title' => 'OpenLayers',
    'website' => 'http://openlayers.org/',
    'version' => '2.11',
    'js' => array(
      drupal_get_path('module', 'tests') . '/OpenLayers.js' => array(),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_library_alter().
 */
function tests_library_alter(&$libraries, $module) {
  return;
}

Comments

zzolo’s picture

Hi @sayayin. You are not even using this module at all. Please see the API for this module to add maps. This module is based around map configurations and displaying that.

If you are looking to manually create an OpenLayers map, this module is way overkill.

sayayin’s picture

Hi @zzolo
Please tell me how I can include a map in my module, and in a form, using the OL API.

For example, in your book, Mapping Drupal, p.82 (I am a registered buyer), you show us an exportable map code.
How I can inject it in the code of my module to display this map?

$default = new stdClass();
$default->api_version = 1;
$default->name = 'default';
$default->title = t('Default Map');
$default->description = t('This is the default map that comes
with the OpenLayers module.');
$default->data = array(
'projection' => '900913',
'width' => 'auto',
'height' => '400px',
'default_layer' => 'osm_mapnik',
'center' => array(
'initial' => array(
'centerpoint' => '0,0',
'zoom' => '2'
)
),
'displayProjection' => '4326',
'maxExtent' => openlayers_get_extent('4326'),
'behaviors' => array(
'openlayers_behavior_panzoombar' => array(),
'openlayers_behavior_layerswitcher' => array(),
'openlayers_behavior_attribution' => array(),
'openlayers_behavior_keyboarddefaults' => array(),
'openlayers_behavior_navigation' => array(),
),
'layers' => array(
'osm_mapnik' => 'osm_mapnik',
)
);
zzolo’s picture

The basic steps are:

1) Make a map in the interface (your exportable).
2) Call openlayers_load_map($map_name); (or something like that)

Please see the documentation that comes with the module:

http://drupalcode.org/project/openlayers.git/blob/refs/heads/7.x-2.x:/do...
http://drupalcode.org/project/openlayers.git/blob/refs/heads/7.x-2.x:/do...
http://drupalcode.org/project/openlayers.git/blob/refs/heads/7.x-2.x:/op...

sayayin’s picture

@zzolo : sorry, really sorry but I want & I need to perform that simple operation :
- I create the map with center and zoom,
- I add in my module code

	openlayers_include();
	drupal_add_js(drupal_get_path('module', 'openlayers')); 
	openlayers_load_map('dogo')

;
but apache's log tell me
"Error: Call to undefined function openlayers_load_map() ".
I am stupid surely but please give me a path to perform it.

steinmb’s picture

You need to tell PHP what code that define function openlayers_load_map() else nothing works.

akalam’s picture

This worked for me:

http://trac.osgeo.org/openlayers/wiki/Release/2.11/GoogleMaps37

so, I added at the end of my views-view--page-view-name.tpl.php:

drupal_add_js('
OpenLayers.Layer.Google.v3.repositionMapElements = function() {
    // This is the first time any Google layer in this mapObject has been
    // made visible.  The mapObject needs to know the container size.
    google.maps.event.trigger(this.mapObject, "resize");
    
    var div = this.mapObject.getDiv().firstChild;
    if (!div || div.childNodes.length < 3) {
        this.repositionTimer = window.setTimeout(
            OpenLayers.Function.bind(this.repositionMapElements, this),
            250
        );
        return false;
    }

    var cache = OpenLayers.Layer.Google.cache[this.map.id];
    var container = this.map.viewPortDiv;
    
    // move the ToS and branding stuff up to the container div
    // depends on value of zIndex, which is not robust
    for (var i=div.children.length-1; i>=0; --i) {
        if (div.children[i].style.zIndex == 1000001) {
            var termsOfUse = div.children[i];
            container.appendChild(termsOfUse);
            termsOfUse.style.zIndex = "1100";
            termsOfUse.style.bottom = "";
            termsOfUse.className = "olLayerGoogleCopyright olLayerGoogleV3";
            termsOfUse.style.display = "";
            cache.termsOfUse = termsOfUse;
        }
        if (div.children[i].style.zIndex == 1000000) {
            var poweredBy = div.children[i];
            container.appendChild(poweredBy);
            poweredBy.style.zIndex = "1100";
            poweredBy.style.bottom = "";
            poweredBy.className = "olLayerGooglePoweredBy olLayerGoogleV3 gmnoprint";
            poweredBy.style.display = "";
            cache.poweredBy = poweredBy;
        }
        if (div.children[i].style.zIndex == 10000002) {
            container.appendChild(div.children[i]);
        }
    }

    this.setGMapVisibility(this.visibility);
};
', 'inline');
Viveks’s picture

I have two view for the openlayers map.
this is working fine on the front page view , but it is not working in the inner page , so What can i do, in it pls help me!!!

pol’s picture

Status: Active » Closed (fixed)

Read links in comment #3.