Hi Team,
i´ve genareted a behavior for adding a button to an map, which allows you to add a wmc.xml to the map. This works really fine, but i do not get a button. So maybe some of you can help me. WHen it works i think we can add this feature to to grand openlayers module.

That is my .info:

name = OpenLayers - Import WMC
description = add an control Icon, which opens a file dialog to open and add a Web Map Coverage
core = 7.x
dependencies[] = openlayers
package = AndyLicht
files[] = behaviors/importwmc.inc

That is my .module:

<?php
function importwmc_openlayers_behaviors(){
return array(
	'openlayers_andylicht_behavior_importwmc' => array(
		'type' => 'layer',
		'title' => t('AndyLicht - Import WMC'),
		'description' => t('add an control Icon, which opens a file dialog to open and add a Web Map Coverage'),
		'behavior' => array(
			'path' => drupal_get_path('module','importwmc').'/behavior',
			'file' => 'importwmc.inc',
			'class' => 'openlayers_andylicht_behavior_importwmc',
			'parent' => 'openlayers_behavior',
		),
	),
);
}

That is the .inc

class openlayers_andylicht_behavior_importwmc extends openlayers_behavior {
public function options_init(){
	return array();
}
public function render(&$map){
	drupal_add_css(drupal_get_path('module','importwmc').'/behavior/importwmc.css');
	drupal_add_js(drupal_get_path('module','importwmc').'/behavior/importwmc.js');
	return $this->options;	
}
}

That is the css-file:

.importwmc-panel{
bottom:5px;
left:5px;
z-index:999999;
}

.importwmc-buttonItemActive{
background-color:#EEEEEE;
border: 1px solid #666666;
color: #222222;
border-radius: 3px;
height: 2em;
width: 6em;
text-align:center;
}

.importwmc-buttonItemActive:after{
content:"ImportWMC";
}

And that is the important javascript:

(function($){

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 20;
OpenLayers.Util.onImageLoadErrorColor = "transparent";

var layerOptions = 
	{
        isBaseLayer: false,
        singleTile: true,
        buffer: 0,
        ratio: 1
    };
        
var format = new OpenLayers.Format.WMC({'layerOptions':layerOptions});
Drupal.behaviors.openlayers_andylicht_behavior_importwmc = {
	'attach':function(context,setting){
		var data = $(context).data('openlayers');
		if(data&&data.map.behaviors['openlayers_andylicht_behavior_importwmc']){
			var options = data.map.behaviors['openlayers_andylicht_behavior_importwmc'];
			var button = new OpenLayers.Control.Button({
				displayClass:'importwmc-button',
				title:Drupal.t('ImportWMC'),
				trigger: function(){
					Drupal.behaviors.openlayers_andylicht_behavior_importwmc.importwmc(data.openlayers);
				}
			});
			var panel = new OpenLayers.Control.Panel({
				displayClass:'importwmc-panel',
				defaultControl:button
			});
			data.openlayers.addControl(panel);
			panel.activate();
			console.log(data);
			//Drupal.behaviors.openlayers_andylicht_behavior_importwmc.importwmc(data.openlayers); only to test becaus i can not see a button
		}
	},
	'importwmc':function(map){
		var fileSelector = $('<input type="file" />').bind('change', function(e)
		{
			var file = this.files[0];
			var reader = new FileReader();
			reader.readAsBinaryString(file);
			reader.onloadend = function() 
			{
                extrawmc = reader.result; 
				extracontext = format.read(extrawmc,{map:map});
				for (var i=0; i<extracontext.layers.length;i++) 
				{ 
					var layer = extracontext.layers[i]; 
					layer.params.TRANSPARENT = 'TRUE'; 
				}
				map.addLayers(extracontext.layers);
            }
        });
        fileSelector.click();
        return false;
	}
};
})(jQuery);

Thanks for helping me.
You can download it from here or you can use/test the one in the attachment

Comments

AndyLicht’s picture

Sorry, I´ve forggoten to wrote which kind of error i get:

SyntaxError: missing ) after argument list  //sites/all/libraries/openlayers/OpenLayers.js?muna80 (row 89 column 57)
alert($(this).parent().parent().parent().attr("class");

I am using OpenLayers 2.12

AndyLicht’s picture

I´ve finished the first version of this module, I will publish it in the evening on here.
When I learned how to publishing datas on drupal.org, i will do it.