From 131b47868d48573b98ca885f00bd3d797784ad9d Mon Sep 17 00:00:00 2001 From: Pol Dell'Aiera Date: Wed, 1 Aug 2012 16:53:02 +0200 Subject: [PATCH] Enable Microsoft Bing layers. --- includes/openlayers.layers.inc | 36 ++++++- plugins/layer_types/openlayers_layer_type_bing.inc | 109 ++++++++++++++++++++ plugins/layer_types/openlayers_layer_type_bing.js | 22 ++++ 3 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 plugins/layer_types/openlayers_layer_type_bing.inc create mode 100644 plugins/layer_types/openlayers_layer_type_bing.js diff --git a/includes/openlayers.layers.inc b/includes/openlayers.layers.inc index dc4ade7..0a36fad 100644 --- a/includes/openlayers.layers.inc +++ b/includes/openlayers.layers.inc @@ -148,14 +148,40 @@ function _openlayers_openlayers_layers() { $layer = new stdClass(); $layer->api_version = 1; - $layer->name = 'virtualearth_street'; - $layer->title = 'Virtual Earth Street'; - $layer->description = 'Virtual Earth (Bing) street tiles.'; + $layer->name = 'Bing_Road'; + $layer->title = 'Bing Road'; + $layer->description = 'Bing Road tiles.'; $layer->data = array( 'baselayer' => TRUE, - 'type' => 'street', + 'type' => 'Road', 'projection' => array('900913'), - 'layer_type' => 'openlayers_layer_type_virtualearth', + 'layer_type' => 'openlayers_layer_type_bing', + ); + $layers[$layer->name] = $layer; + + $layer = new stdClass(); + $layer->api_version = 1; + $layer->name = 'Bing_Aerial'; + $layer->title = 'Bing Aerial'; + $layer->description = 'Bing Aerial tiles.'; + $layer->data = array( + 'baselayer' => TRUE, + 'type' => 'Aerial', + 'projection' => array('900913'), + 'layer_type' => 'openlayers_layer_type_bing', + ); + $layers[$layer->name] = $layer; + + $layer = new stdClass(); + $layer->api_version = 1; + $layer->name = 'Bing Hybrid'; + $layer->title = 'Bing Hybrid'; + $layer->description = 'Bing Hybrid tiles.'; + $layer->data = array( + 'baselayer' => TRUE, + 'type' => 'AerialWithLabels', + 'projection' => array('900913'), + 'layer_type' => 'openlayers_layer_type_bing', ); $layers[$layer->name] = $layer; diff --git a/plugins/layer_types/openlayers_layer_type_bing.inc b/plugins/layer_types/openlayers_layer_type_bing.inc new file mode 100644 index 0000000..d2b0219 --- /dev/null +++ b/plugins/layer_types/openlayers_layer_type_bing.inc @@ -0,0 +1,109 @@ + t('Bing'), + 'description' => t('Microsoft Bing'), + 'layer_type' => array( + 'file' => 'openlayers_layer_type_bing.inc', + 'class' => 'openlayers_layer_type_bing', + 'parent' => 'openlayers_layer_type', + ), +); + +/** + * OpenLayers VirtualEarth Layer Type class + */ +class openlayers_layer_type_bing extends openlayers_layer_type { + function __construct($layer = array(), $map = array()) { + parent::__construct($layer, $map); + if (isset($this->data)) { + $this->data += $this->options_init(); + } + else { + $this->data = $this->options_init(); + } + } + + /** + * Provide initial values for options. + */ + function options_init() { + return array( + 'layer_type' => 'bing', + 'layer_handler' => 'bing', + 'key' => variable_get('openlayers_layers_bing_api', ''), + 'projection' => array('900913'), + 'baselayer' => TRUE, + 'type' => 'Road', + ); + } + + /** + * Options form which generates layers + */ + function options_form($defaults = array()) { + $warning = (!variable_get('openlayers_layers_bing_api', FALSE)) ? + array('#value' => t('WARNING: Your Bing API key is not set. + Map including Bing layers will break until it is set correctly.') + ) : NULL; + + $bing_layer_types = array( + 'Road' => 'Road', + 'AerialWithLabels' => 'Hybrid', + 'Aerial' => 'Aerial', + ); + + return array( + 'type' => array( + '#title' => t('Bing Layer Type'), + '#type' => 'select', + '#default_value' => isset($this->data['type']) ? + $this->data['type'] : 'Road', + '#options' => $bing_layer_types + ), + 'layer_type' => array( + '#type' => 'hidden', + '#value' => 'openlayers_layer_type_bing' + ), + $warning + ); + } + + /** + * Layer-type-wide settings form + */ + function settings_form() { + return array( + 'openlayers_layers_bing_api' => array( + '#type' => 'textfield', + '#title' => t('Bing API Key'), + '#default_value' => variable_get('openlayers_layers_bing_api', ''), + '#description' => t('Get a MS Bing API Key', + array('@microsoft' => 'http://bingmapsportal.com')) + ) + ); + + } + + /** + * Render. + */ + function render(&$map) { + static $bing_maps_included; + + if (!isset($bing_maps_included)) { + drupal_add_js(drupal_get_path('module', 'openlayers') . + '/plugins/layer_types/openlayers_layer_type_bing.js'); + $bing_maps_included = TRUE; + } + return $this->options; + } +} diff --git a/plugins/layer_types/openlayers_layer_type_bing.js b/plugins/layer_types/openlayers_layer_type_bing.js new file mode 100644 index 0000000..1d2424a --- /dev/null +++ b/plugins/layer_types/openlayers_layer_type_bing.js @@ -0,0 +1,22 @@ + +/** + * Process MS Bing Layers + * + * @param layerOptions + * Object of options. + * @param mapid + * Map ID. + * @return + * Valid OpenLayers layer. + */ +Drupal.openlayers.layer.bing = function(title, map, options) { + var styleMap = Drupal.openlayers.getStyleMap(map, options.drupalID); + + options.sphericalMercator = true; + options.projection = "EPSG:900913"; + options.maxExtent = new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34); + + var layer = new OpenLayers.Layer.Bing(options); + layer.styleMap = styleMap; + return layer; +}; -- 1.7.3.4