Using the Mapbender API

From MapbenderWiki

Jump to: navigation, search

This is a tutorial for Mapbender 2.7 and newer (note: this equals trunk at the moment, July 2009).

You can use the functionality of other modules by using their API functions. There are two options, DOM access or object access. DOM access is more elegant (especially if you already have a jQuery collection) and it's less error prone. The object access is faster, but you need to check if the module exists, which could make the could verbose.

Contents

access via DOM

First, select the module you want to address via jQuery

$("#mapframe1");

There are also other custom selector, like "select all maps"

$(":maps");

or select all modules

$(":modules");

You can of course combine these selectors with existing jQuery selectors, like

$("img:modules");

the mapbender function

Use the mapbender method to access the API, there are several use cases

execute an API function

Include a function as an argument.

$("#mapframe1").mapbender(function () {
});

This function will be executed within the scope of the API, so this refers to the API object. For example, this will cause the map to pan northwards.

$("#mapframe1").mapbender(function () {
    this.pan("N");
});

In this case, the mapbender function is chainable.

get the value of an attribute

Include the attribute name as string

var name = $("#mapframe1").mapbender("name");

get the API object

No argument.

var apiObject = $("#mapframe1").mapbender();

I think this approach is much nicer: You do not need to check whether the module exists, and you do not need to struggle with the Mapbender API tree.

access via object

You will find other modules within the Mapbender namespace under modules

Mapbender.modules

You can pick the module by specifying the ID in brackets, followed by the API function. For example, the target might contain the ID of a map module. You would trigger a map request by

Mapbender.modules[options.target].setMapRequest()
Note: "Mapbender" is written with a capital "M", because it's a constructor. However, 
      "modules" is written with a lower case "m", because it's an array! Take a look at the
      Code conventions if you want to learn more about this.

Anytime you call a function not declared within your module without the Mapbender reference, you are either doing something wrong, or using a deprecated function. Do not use deprecated functions, use the equivalent within the Mapbender namespace.

Recommended further reading

Views
Personal tools