Ambita Map
ambita-map.js is a JavaScript API / library that is built as a Leaflet wrapper / plugin.
Please refer to Leaflet documentation for further questions regarding Leaflet. It is also advised to take a look at the great examples that are available on the Leaflet webpage and goes deeply into extending Leaflet and adding new exciting functionality. ambita-map.js provides the same extendability and functionality with various extra features added on top.
ambita-map.js uses Leaflet version 1.2.0 which is a stable release.
Get started
To get started include the ambita-map.js library’s CSS file and JavaScript file in your HTML:
Include
ambita-map.css
file from the library in the<head>
section of the HTML:<link rel="stylesheet" href="https://static.ambita.com/ambita-map/latest/ambita-map.css">
Include
ambita-map.js
file from the library in the<head>
or<body>
section of the HTML:<script type="text/javascript" src="https://static.ambita.com/ambita-map/latest/ambita-map.js"></script>
Optionally include
font-awesome.css
file from the Font Awesome library in the<head>
section of the HTML to be able to display the default icons used by the map properly (not a requirement since the default icons used by the map can be overridden, see the map options):<link rel="stylesheet" type="text/css" href="https://unpkg.com/font-awesome@4.7.0/css/font-awesome.css">
Add a
<div>
element with a given id in the<body>
section of the HTML where the map should be displayed:<div id="map"></div>
Define height of the added
<div>
element to properly display the map:#map { height: 600px; }
Initialize the map using the added
<div>
element or itsid
, a valid Ambita authorization token string and map options that are completely optional. More about token handling and retrieving a valid Ambita authorization token is described in the next section of this documentation:L.ambita.map('map', 'VALID TOKEN', { center: [60, 10], zoom: 14 })ja
See the style guide below on how to style the individual elements on the map.
Token handling
To be able to use the map a valid Ambita authorization token is required. To retrieve a valid Ambita authorization token a POST
method call against authentication API token service has to be made. Opening the authentication resource links in the browser will give an HTTP
401 Unauthorized error, unless a valid Ambita authorization token is set on the Authorization
parameter using the request headers.
Valid Ambita authorization token can be generated using proper credentials and the following resource URL
https://beta-api.ambita.com/authentication/v2/token or https://api.ambita.com/authentication/v2/token if the production API is being used (ambita-map.js uses beta-api
by default, switching to production API is done through map options). Using JavaScript requires to do a simple POST
method call passing the authorization parameters as the URL
parameters to the authentication API’s token service https://beta-api.ambita.com/authentication/v2/token?grant_type=password&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&username=API_USERNAME&password=API_PASSWORD
Example of the request
Just for the sake of the example the POST
method call is shown using jQuery. jQuery is not a requirement by any means.
var url = 'https://beta-api.ambita.com/authentication/v2/token?grant_type=password&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&username=API_USERNAME&password=API_PASSWORD';
$.post(url, function (data, status) {
// work with the JSON response here
});
After a request to generate a token is made a JSON response will be returned containing the access_token
.
Example of the response
The map authorization token consists of token_type
and access_token
combined and separated with a whitespace. The response from the authentication API also consists of the expiration time of the access_token
in seconds, a refresh_token
that could be used to generate a new access_token
once it has expired and an array of scopes the retrieved access_token
has access to. Using the example response, the authorization token would look like this Bearer b8e7f3c5-f8c6-4011-9bd6-0282a1894df7
. The authorization token needs to be passed to the map initialization function and by default expires after an hour. When the token has expired the map fires a tokenrequired
event. To continue to use all the map features a new authorization token needs to be generated and passed to the map using map.authenticate(token)
function.
To retrieve a new Ambita authorization token the refresh_token
can be used. Using JavaScript requires to do a simple POST
method call passing the authorization parameters as the URL
parameters to the authentication API’s token service https://beta-api.ambita.com/authentication/v2/token?grant_type=refresh_token&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&refresh_token=REFRESH_TOKEN
Example of the request can be found above.
Token example code
Full example with a valid token
Live demo of the example can be found here JSFiddle
Example of working with the map after initialization
To work with the newly created map object it should be properly initialized. Every action or function call on the map object should be done after ambitamap:loaded
is fired.
Demo
To be able to work with the demo a valid Ambita authorization token is needed and more information regarding token handling is provided above. A complete demo of the ambita-map.js library and its different features can be found and tested out here ambita-map.html.
API
Creates and returns the map object and configures the map using the passed in map options.
Extends: L.Map
Factory
Option | Type | Description |
---|---|---|
| String or HTMLElement | required The id of a |
| String | required Ambita authorization token in the form of |
| Object | Map options, see below for more details |
Options
Since ambita-map.js is built on top of the Leaflet library it inherits the map options from the library and adds ambita-map.js specific map options as well. Default Leaflet map options can be found in the Leaflet documentation.
Option | Type | Default | Description |
---|---|---|---|
| String | REST API address, for production use https://api.ambita.com | |
| CRS Object | L.ambita.CRS[‘EPSG:25833’] | Map CRS used to show the base and other layers on the map in a different coordinate system than WGS84 |
| Number | 10 | Initial map zoom level, a number between 1 and 17 |
| Number[] | [60, 10] | Initial center of the map, an array of two coordinates in WGS84 coordinate system [latitude, longitude] |
| Boolean | true | Enable keyboard controlled movement of the map |
| Boolean | true | Enable inertia when panning the map using mouse |
| Boolean | false | Show attribution control on the map |
| Object | undefined | Options to customize the attribution control, see below for options |
| Number | 3 | Minimum zoom level of the map |
| Number | 17 | Maximum zoom level of the map |
| String | undefined | Authorization token from the Ambita authentication API (must set |
| String | undefined | GeoData token (fetched automatically on map initialization if not specified) |
| Number | 3 | Number of times |
| String | ‘selection-cadastre’ | Default SVG path class name for geometries added to the map |
| Boolean | true | Enable the detailed height legend control |
| Number | 10 | The default buffer for |
| Boolean | false | Show the default Leaflet zoom control |
| Boolean | true | Show the property layer on the map |
| Boolean | undefined | Show the default Leaflet fullscreen control |
| Boolean | true | Show the layer control button |
| String | ‘topleft’ | Position of the layer control button |
| String | L.ambita.Statics.SVG_BUTTONS.layers | HTML for the layer control button |
| Boolean | false | Show the scale control at the bottom left corner of the map |
| Boolean | true | Show the Powered by Ambita control at the bottom right corner of the map |
| Boolean | true | Enable the editing of geometries functionality on the map |
| String | ‘selection-lineguide’ | Class name of the selection tools line guide |
| Class | middleMarkerCustom | Custom class of the selection tools middle markers |
| Object | { show: false } | Options to customize the selection tools, see below for options |
| Object | undefined | Options to customize the POC layer, see below for options |
| Object | undefined | Options to customize the POB layer, see below for options |
| String | undefined | Municipality code of the Norway municipality to fetch and draw a border of |
| Boolean | true | Show the zoom control buttons |
| Boolean | false | Animate the display of zoom, layer, selection tools controls |
| String | ‘/authentication/v2/token’ | Path to authorization token resource |
| String | ‘/authentication/v2/geodatatoken’ | Path to GeoData token resource |
| String | ‘arcgis/rest/services/’ | Path to ArcGIS REST services |
| String | ‘Geocache_UTM33_EUREF89/’ | Path to UTM Cache service |
| String | ‘Geomap_UTM33_EUREF89/’ | Path to UTM Dynamic service |
| String | ‘/MapServer/’ | Path to map server |
| String | ‘Geocache’ | Name of UTM Cache service |
| String | ‘Geomap’ | Name of UTM Dynamic service |
| String | ‘Basis’ | Name of the base map layer |
| String | ‘Bilder’ | Name of the map orto layer |
| String | ‘Eiendom3’ | Name of the map property layer |
| String | UTM Cache server address | |
| String | UTM Dynamic server address | |
| Boolean | false | Enable static map (no interactivity) |
| Boolean | false | Disable panning of the map |
| Number | 200 | Maximum height of the feature popup |
| Number | 300 | Maximum width of the feature popup |
| Number | 100 | Minimum width of the feature popup |
| Point | L.point(0, 0) | Offset of the feature popup |
| Number | 60 | GeoData token expiration time in minutes |
| Object[] | undefined | Array of customizable Ambita controls, see below for options |
| Object | undefined | Options to customize the fullscreen control, see below for options |
| Object | undefined | Options to customize the scale control, see below for options |
Attribution control options
Option | Type | Default | Description |
---|---|---|---|
| String | ‘bottomright’ | Position of the attribution control |
| String | ’’ | Prefix text to the attribution control |
Selection tools options
Selection tools can be used to draw different geometries on the map.
Option | Type | Default | Description |
---|---|---|---|
| Boolean | Show the selection tools on the map, map option |
|
| String | ‘bottomleft’ | Position of the selection tools control |
| String | ‘3’ | GeoData identify layer Id |
| Boolean | false | Enable |
| Boolean | true | Enable |
| Boolean | true | Enable filtering on sections in owner list |
| Boolean | true | Enable filtering on sections in neighbor list |
| String | ‘selection-main-cadastre’ | Neighbor list main cadastre CSS class name |
| Number | 500 | Maximum owners count returned from the service for owner list |
| Number | 35 | Maximum neighbors count returned from service for neighbor list |
| Number | 100 | Minimum area size of polygon selection |
| Number | 100 | Minimum area size of rectangle selection |
| Number | NaN | Maximum area size of selection |
| String | ‘Linjene kan ikke krysses’ | Tooltip for the intersecting polygon error |
| String | ‘Utvalget krysser kommunegrenser’ | Tooltip for the polygon selection crossing municipality border |
| String | ‘Ingen flere matrikkler kan legges til utvalget’ | Tooltip for the maximum owners count error |
| String | ‘Valgt område er for stort, for mange eiendommer returnert’ | Tooltip for the maximum owners count error when doing single property selections |
| String | ‘Ingen flere matrikkler kan legges til utvalget’ | Tooltip for the maximum neighbors count error |
| String | ‘Hoved matrikkelen finnes ikke, så det er ingen nabo listen tilgjengelig’ | Tooltip for the main cadastre error |
| String | ‘Valgt område er for lite’ | Tooltip for the minimum area size error for polygon selection |
| String | ‘Valgt område er for lite’ | Tooltip for the minimum area size error for rectangle selection |
| String | ‘Valgt område er for stort’ | Tooltip for the maximum area size error for polygon selection |
| String | ‘Valgt område er for stort’ | Tooltip for the maximum area size error for rectangle selection |
| String | ‘Utvalget har gjort må være helt inne Norge’ | Tooltip for the selection outside of Norway error |
| String | ‘Utvalget må være enten helt innenfor eller utenfor Oslo’ | Tooltip for the selection intersecting with Oslo error |
| String | ‘Utvalget er utenfor kommunen’ | Tooltip for the selection crossing restricted municipality polygon error |
| String | ‘fa-mouse-pointer’ | Font Awesome icon for the cadastre toggle button |
| String | ‘Legg til eller fjern matrikkel fra utvalget’ | Title of the cadastre toggle button |
| String | ‘Klikk på kartet for å legge til en matrikkel, klikk på en matrikkel for å fjerne den’ | Tooltip for the cadastre toggle button |
| String | ‘fa-expand’ | Font Awesome icon for the polyline button |
| String | L.ambita.Statics.SVG_BUTTONS.polyline | HTML for the polyline button, supersedes the icon option |
| String | ‘Tegn en linje’ | Title of the polyline button |
| String | ‘Klikk i kartet for å starte en linje’ | Tooltip for the polyline selection start point |
| String | ‘Klikk i kartet for å skifte retning’ | Tooltip for the polyline selection continue point |
| String | ‘Klikk på det siste punktet for å avslutte linjen’ | Tooltip for the polyline selection finish point |
| String | ‘fa-map’ | Font Awesome icon for the polygon button |
| String | L.ambita.Statics.SVG_BUTTONS.polygon | HTML for the polygon button, supersedes the icon option |
| String | ‘Tegn et polygon’ | Title of the polygon button |
| String | ‘Klikk i kartet for å starte polygonet’ | Tooltip for the polygon selection start point |
| String | ‘Klikk i kartet for å fortsette polygonet’ | Tooltip for the polygon selection continue point |
| String | ‘Klikk på det siste punktet for å avslutte polygonet’ | Tooltip for the polygon selection finish point |
| String | ‘fa-stop’ | Font Awesome icon for the rectangle button |
| String | L.ambita.Statics.SVG_BUTTONS.rectangle | HTML for the rectangle button, supersedes the icon option |
| String | ‘Tegn et rektangel’ | Title of the rectangle button |
| String | ‘Trykk og dra i kartet for å starte rektangelet’ | Tooltip for the rectangle selection start point |
| String | ‘Dra i hjørnet eller klikk på det for å fullføre rektangelet’ | Tooltip for the rectangle selection finish point |
| String | ‘fa-trash’ | Font Awesome icon for the delete button |
| String | ‘Slett utvalget’ | Title of the delete button |
| String | ‘Dra i et punkt for å endre’ | Tooltip for the vertex drag for rectangle selection |
| String | ‘Dra i et punkt for å endre eller klikk for å slette det’ | Tooltip for the vertex drag or click for polyline and polygon selections |
| String | ‘Velg eiendom på kartet’ | Tooltip for choosing cadastre plot |
POC / POB layer options
POC and POB layers work with the Realty REST API to display various data on the map.
Option | Type | Default | Description |
---|---|---|---|
| Object | undefined | Object containing all the POC / POB layer options |
| Boolean | true | Enable the POC / POB layer |
| String | REST API address, for production use https://api.ambita.com | |
| Number | 14 | Minimum zoom level when the layer is visible |
| Number | 17 | Maximum zoom level when the layer is visible |
| Boolean | true | Show free market data only |
| Number | 2010 | Date from which the data should be displayed |
| Number | 2020 | Date to which the data should be displayed |
| String | ’’ | Show ground usage data |
| String | ‘DD/MM-YYYY’ | Date format |
| Number | 200 | Maximum height of the feature popup |
| Number | 300 | Maximum width of the feature popup |
| Number | 100 | Minimum width of the feature popup |
| Point | L.point(0, 0) | Offset of the feature popup |
| String | undefined | Authorization token from the Ambita authentication API (if a different authorization token than the one used by the map is needed) |
Ambita controls options
Option | Type | Default | Description |
---|---|---|---|
| String | ‘topleft’ | Position of the custom Ambita control |
| Boolean | false | Enable the control on initialization |
| String | undefined | Title of the custom Ambita control |
| String | undefined | Icon of the custom Ambita control |
| Function | undefined | Function to be called on control click event |
| Boolean | undefined | Enable one time click functionality |
| Function | undefined | Function to be called on map click event |
Fullscreen control options
Option | Type | Default | Description |
---|---|---|---|
| String | ‘topleft’ | Position of the fullscreen control |
| String | ’ | HTML for the fullscreen control button |
| String | ‘Fullskjerm’ | Title of the fullscreen control button |
| Boolean | false | Forces fullscreen control as a separate button, otherwise a part of zoom control |
| Boolean | false | Forces pseudo fullscreen, true by default if displayed in an iframe |
| HTMLElement | Map Container | Container to display the pseudo fullscreen in |
Scale control options
Option | Type | Default | Description |
---|---|---|---|
| String | ‘bottomleft’ | Position of the scale control, cannot be modified |
| Number | 100 | Maximum width of the scale control in pixels |
| Boolean | true | Show the metric scale |
| Boolean | true | Show the imperial scale, if no scale control options passed this is false |
| Boolean | false | Update on |
Feature layer options
Option | Type | Default | Description |
---|---|---|---|
| String | ‘selection-cadastre’ | Default SVG path class name for features added to the map |
| String | undefined | Adds the feature layer to the layer control using title provided, not used by |
| String | undefined | Name of the event to be fired when the layer is shown on the map, not used by |
| Boolean | undefined | Enable animation of |
| Boolean | undefined | Clears the previously added feature layer by the same name, not used by |
| Boolean | undefined | Clears all previously added feature layers, not used by |
| Boolean | undefined | Zooms the map to the added feature layer |
| Number | undefined | Sets the padding of the map container that should not be accounted for when zooming the map to the added feature layer |
| Boolean | undefined | Shows an HTML label in the center of the feature layer, only works if |
| Function | undefined | Function that receives feature as a parameter and returns an HTML to display on the feature layer |
| Layer | undefined | Layer group on which the newly added feature layer would be added on, not used by |
Neighbor list options
Option | Type | Default | Description |
---|---|---|---|
| String | ‘neighborlist’ | Name of the neighbor list layer |
| Boolean | true | Should the cadastre sections be filtered out |
| Boolean | undefined | Should the main cadastre be shown on the map |
| Boolean | undefined | Should the buffer around the main cadastre be shown on the map |
| Boolean | undefined | Same description as in |
| Function | undefined | Same description as in |
| Boolean | undefined | Same description as in |
| Boolean | undefined | Zooms the map to the main cadastre layer |
| Number | undefined | Same description as in |
Events
ambita-map.js library fires different events based on the actions done using the map. These events can be used to implement further interactability with the map.
Example usage of events fired by the map
Selection tools fire events when they are used so it is possible to act on the different actions done using line, polygon or rectangle selection tools on the map based on the event fired.
Example JavaScript code using selection tools events on an already created map
instance is shown below:
Map events
In addition to all of the custom ambita-map.js map events that are listed below the map also fires the default map events provided by the Leaflet library that can be found here.
Event | Description | Returned value |
---|---|---|
| Fired when initialization of the map is done |
|
| Fired when authorization with Ambita authentication API has failed |
|
| Fired when authorization with Ambita authentication API has failed 3 consecutive times |
|
| Fired when a feature layer is shown on the map using the | Layer just added to the map |
| Fired when feature layers are shown on the map using the | Layer just added to the map |
| Fired when a marker cluster layer is shown on the map using the | Layer just added to the map |
| Fired when any of the Ambita tasks have been executed | Object containing retrieved feature collection |
| Fired when default layer options are used and a feature is clicked that has a type option | Object containing clicked feature |
| Fired when | Object containing retrieved feature collection |
| Fired when | Object containing retrieved feature collection |
| Fired when the map exits fullscreen mode |
|
| Fired when the map enters fullscreen mode |
|
| Fired when the input cadastre is defined incorrectly for neighbor list |
|
| Fired when the |
|
| Fired when the query task to return owners is done for owner list | Object containing retrieved feature collection |
| Fired when the count task returns owner count that exceeds the maximum owners count for owner list |
|
| Fired when the neighbor list layer has been loaded |
|
Selection tools events
Event | Description | Returned value |
---|---|---|
| Fired when a new selection is started | Type of selection editor that is started |
| Fired when the selection is ended using mouse double click | GeoJSON of the selection made on the map |
| Fired when the selection is modified using mouse | GeoJSON of the selection made on the map |
| Fired when the selection is started to be modified using mouse | GeoJSON of the selection made on the map |
| Fired when the selection is deleted |
|
| Fired when the selection is invalid |
|
| Fired when the owner list is being started to retrieve |
|
| Fired when the owner list is cleared |
|
| Fired when the owner list is updated | Object containing the GeoJSON of the selection made on the map and selected cadastres |
| Fired when the owner list is deleted |
|
| Fired when the neighbor list is cleared |
|
| Fired when the neighbor list is updated | Object containing the GeoJSON of the selection made on the map, selected cadastres and neighbor list selected plot |
| Fired when the neighbor list is deleted |
|
| Fired when the main cadastre plot has been chosen for the neighbor list | Object containing the cadastre number |
| Fired when the main plot is selected for the neighbor list | Object containing feature layer of the selected plot |
| Fired when the main plot is reset for the neighbor list |
|
| Fired when the main plots are shown for the neighbor list | Object containing feature layers of the shown plots |
Methods
Method | Returns | Description |
---|---|---|
| AmbitaMap | Tries to authenticate all the layers using the Ambita authorization token passed in and get GeoData token to be able to work with the map features. Fires |
| AmbitaMap | Calls GeoData query service using the cadastre number string |
| AmbitaMap | Uses the GeoJSON feature or feature collection passed in. The GeoJSON can be of any valid GeoJSON geometry. For custom markers on icons use |
| void | Adds the selection controls to the map, only works if |
| void | Removes the selection controls from the map, only works if |
| void | Removes the Ambita custom controls from the map |
| AmbitaMap | Shows the passed in feature on the map |
| AmbitaMap | Shows the passed in feature collection on the map |
| AmbitaMap | Shows the passed in cadastre feature on the map |
| void | Shows cadastre plots layer using the cadastre number from the feature passed in |
| void | Shows municipality layer using the municipality feature passed in |
| AmbitaMap | Shows the passed in feature collection of markers as a marker cluster layer |
| void | Shows the neighbor list using the cadastre number passed in |
| AmbitaMap | Shows the passed in marker feature on the map |
| void | Adds the CSS class |
| void | Removes the CSS class |
| void | Zooms the map to the feature layer passed in |
| void | Clears a feature layer added to the map by the name passed in |
| void | Clears all feature layers added to the map |
| AmbitaMap | Calls GeoData identify service using the feature passed in and shows the retrieved cadastre on the map. Specifying the |
| String | Gets the currently used REST API address |
| void | Fires the |
| Boolean | Checks if the map is currently in fullscreen mode |
| String | Gets the geohash string for the current map center |
| void | Zooms the map to the geohash string passed in |
| void | Calls the GeoData query service using the parameters passed in and shows the retrieved neighbor list on the map. Specifying the |
| void | Calls GeoData query service using the municipality code passed in and shows the retrieved municipality on the map |
|
| Calls GeoData query service and gets the municipalities from the current map center |
|
| Calls GeoData query service using the feature passed in and gets the municipalities containing the feature |
Example usage of map.showMarkerLayer
To display markers on the map map.showMarkerLayer(features)
should be called.
It is possible to do different types of actions when a feature is clicked on the map:
run a callback function
fire an event
show a popup
Run a callback function
The callback
parameter should be set in feature properties.
Fire an event
The type
parameter should be set in feature properties. This will fire a featureClick
event which will contain the clicked feature
Show a popup
The popup
and text
parameters should be set in feature properties. Text can be any valid text or HTML.
Styling the map elements
Style the different elements (buttons, controls, etc.) shown on the map using plain CSS.
Identifier | Description |
---|---|
| Style for buttons and other controls |
| Style for buttons and other controls on hovering |
| Style for zoom in control |
| Style for zoom out control |
| Style for the element using CSS identifier |
Example stylesheet
Versioning
Stable releases are available here: https://static.ambita.com/ambita-map/
Beta releases are available here: https://beta-static.ambita.com/ambita-map/
The latest stable release is available here: https://static.ambita.com/ambita-map/latest/
Be aware of caching in the browser when using: https://static.ambita.com/ambita-map/latest/
To break the caching put a random query after the library path: https://static.ambita.com/ambita-map/latest/ambita-map.js?randomText