![]() |
![]() |
|
This series of articles focuses on the theme of "system development." Based on FORUM8's track record in system development, we will introduce various topics each time. In this 24th installment, we will be introducing a web application mapping library.
Introduction to web application map libraries
Introduction
Map libraries are essential for websites and apps, providing the maps we see every day. Map displays are versatile features used in various situations, such as providing directions, showing evacuation routes, and displaying the user's current location using GPS. Because map libraries are so general-purpose, their selection is crucial, and it's important to understand the characteristics of each one.
Therefore, in this issue, we will introduce 4 major web application map libraries (hereinafter referred to as "map libraries").
About the map library
(1) A map library is..
A map library is a convenient component that allows you to display various maps by simply adding a few lines of code. It also has various functions, such as tile layers that divide the map into smaller pieces and display them like tiles, vector layers that draw shapes in vector format, and marker displays that show relevant locations with icons or other symbols.
(2) The map libraries being introduced
This paper introduces 4 map libraries, all of which support features such as displaying tile layers, vector layers, and markers.
Name |
Main display targets |
License |
The latest version |
Leaflet |
2-dimensional map |
Section 2: BSD License |
v1.9.4 |
OpenLayers |
2-dimensional map |
Section 2: BSD License |
v7.5.1 |
CesiumJS |
2-dimensional map |
Apache License, Version 2.0 |
v1.108 |
MapLibre GL JS |
2-dimensional map |
Section 3: BSD License |
v3.3.1 |
Table 1: Comparison of map libraries
Features of each map library
(1)Leaflet
Leaflet※1 is a library released in 2011 that can display two-dimensional maps. Its most notable feature is its lightweight file size; the JavaScript file is only 144KB. OpenLayers, which also specializes in displaying two-dimensional maps like Leaflet, has a file size of 732KB, making it approximately one-fifth the size of Leaflet. The following code example shows how to use Leaflet to display geographic institute tiles※2 as map images.
<link href="https://unpkg.com/leaflet@latest/dist/leaflet.css"
rel="stylesheet"/>
<script src="https://unpkg.com/leaflet@latest/dist/leaflet.js"></script>
<script>
L.map('map', // Specify the ID of the HTML element that will display the map
{
// Set the layer
layers: [
// Specify the URL of the tile layer to be displayed
L.tileLayer('https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png')
],
// Specifies the initial center position and zoom level of the map when it's first displayed
center: [35, 138],
zoom: 5
}
);
</script>
(2)OpenLayers
OpenLayers※3This is a library released in 2006 that can display 2-dimensional maps. Unlike Leaflet, the default coordinate system applied is the Web Mercator coordinate system. Therefore, if you want to use coordinates expressed in latitude and longitude (geographical coordinate system), you need to perform a coordinate transformation as described in the usage examples. The following code example shows how to use OpenLayers to display GSI tiles as map images.
<link href="https://unpkg.com/ol@latest/ol.css"
rel="stylesheet" />
<script src="https://unpkg.com/ol@latest/dist/ol.js"></script>
<script>
new ol.Map({
// Specify the ID of the HTML element that will display the map
target: 'map',
// Set the layer
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
// Specify the URL of the tile layer to be displayed
url: 'https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png'
})
})
],
// Specifies the initial center position and zoom level of the map when it's first displayed
view: new ol.View({
// Convert the coordinate system
center: ol.proj.transform([138, 35], 'EPSG:4326', 'EPSG:3857'),
zoom: 5
})
});
</script>
※ Regarding display in web browsers, it is almost identical to Leaflet, so images have been omitted.
(3)CesiumJS
CesiumJS※4, released in 2012, is a library characterized by its ability to display not only 2-dimensional maps but also the Earth as a three-dimensional sphere. It allows for easy display of three-dimensional terrain and various three-dimensional models such as 3DTiles and glTF. Furthermore, it supports the representation of time-series data and includes various operation panels by default. Additionally, by using the paid service Cesium ion, users can access a range of features, including adding default terrain. The following code example demonstrates how to use CesiumJS to display geographic institute tiles as map images.
<link href="https://unpkg.com/cesium@latest/Build/Cesium/Widgets/widgets.css
rel="stylesheet"/>
<script src="https://unpkg.com/cesium@latest/Build/Cesium/Cesium.js"></script>
<script>
new Cesium.Viewer('map', // Specify the ID of the HTML element that will display the map
{
// Set the layer
imageryProvider: new Cesium.OpenStreetMapImageryProvider({
// Specify the URL of the tile layer to be displayed
url: 'https://cyberjapandata.gsi.go.jp/xyz/std/',
fileExtension: 'png'
}),
To prevent the Ion authentication process from being invoked, set both of these properties to false
baseLayerPicker: false,
geocoder: false
}
).camera.setView({ // Specifies the camera position when the application initially loads
destination: Cesium.Cartesian3.fromDegrees(138, 35, 3000000)
})
</script>
The code described above is displayed in a web browser as follows. In Cesium, the map is projected onto a sphere, so you can confirm that the ground is part of a sphere.
(4)MapLibre GL JS
With the transition of the MapBox GL JS mapping library from a free to a paid service, MapLibre GL JS was forked from the pre-paid version (v1.13.0) and released in 2021※5. In addition to 2D maps, it also supports 3D representations of terrain. Furthermore, you can add 3D models using WebGL libraries such as three.js and babylon.js. The following code example shows how to use MapLibre GL JS to display geographic institute tiles as map images.
<link href="https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.css"
rel="stylesheet"/>
<script src="https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.js"></script>
<script>
new maplibregl.Map({
container: 'map', // Specifies the ID of the HTML element that will display the map
style: {
version: 8,
sources: {
std: { // Define the layer
type: 'raster',
// Specify the URL of the tile layer to be displayed
tiles: [
'https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png'
],
tileSize: 256
}
},
layers: [ // Set the defined layers ]
{
id: 'std',
type: 'raster',
source: 'std'
}
]
},
// Specifies the initial center position and zoom level of the map when it's first displayed
center: [138, 35],
zoom: 4
});
</script>
When displaying the above code in a web browser and changing the viewpoint, the following occurs: Unlike CesiumJS, the ground is flat, but it is possible to change the viewpoint in 3 dimensions.
Finally,
In this article, we introduced 4 map libraries for web applications that can be used free of charge. The code and display examples showed the most basic function of displaying tile layers, but a wide range of features are available, including marker display, 3D shape rendering, map manipulation, and event handling. Furthermore, it is useful to choose different libraries depending on the application. For example, Leaflet, which is lightweight for 2D maps, can be used in conjunction with CesiumJS, which displays 3D shapes, within the same application.
Annotations
※1 Leaflet https://leafletjs.com/ (accessed August 31, 2023)
※2 Geographical Survey Institute tiles: https://maps.gsi.go.jp/development/ichiran.html (accessed August 31, 2023)
※3 OpenLayers https://openlayers.org/ (accessed August 31, 2023)
※4 CESIUM https://cesium.com/platform/cesiumjs/ (accessed August 31, 2023)
※5 MapLibre https://maplibre.org/ (accessed August 31, 2023)
(Featured in the Autumn issue of Up&Coming '23)
|
||||||||
![]() Up&Coming |
LOADING