You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/2019-03-24-how-to-create-a-simple-map-with-a-marker-based-on-openstreetmap.md
+22-20Lines changed: 22 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,11 @@ categories: ["howto"]
3
3
title: How to create a simple map (with a marker) based on OpenStreetMap
4
4
---
5
5
6
+
## Introduction
7
+
6
8
There are 2 most common open-source JavaScript libraries used to display a map : [Leaflet](https://leafletjs.com/) and [OpenLayers](https://openlayers.org/).
7
9
8
-
###Build a simple map using Leaflet
10
+
## Build a simple map using Leaflet
9
11
10
12
This tutorial is based on [Leaflet Quick Start Guide](https://leafletjs.com/examples/quick-start/) and uses version `1.4.0` of the library. I suggest you check if there is a more up-to-date version before proceeding.
11
13
@@ -17,31 +19,31 @@ This tutorial is based on [Leaflet Quick Start Guide](https://leafletjs.com/exam
17
19
crossorigin="">
18
20
```
19
21
20
-
2. Include the Leaflet JavaScript library at the end of the `<body>` section of your HTML page
22
+
1. Include the Leaflet JavaScript library at the end of the `<body>` section of your HTML page
4. Now you can add a `<script>` section at the end of the `<body>` section (after the `<script>` that loads Leaflet JavaScript library). All the following JavaScript code needs to go in that new `<script>` section.
36
+
1. Now you can add a `<script>` section at the end of the `<body>` section (after the `<script>` that loads Leaflet JavaScript library). All the following JavaScript code needs to go in that new `<script>` section.
35
37
36
-
5. Initialize the map
38
+
1. Initialize the map
37
39
38
40
```js
39
41
var map =L.map('map').setView([50.84673, 4.35247], 12);
40
42
```
41
43
42
44
The map will be displayed in `<div id="map">` element and will be centered on Brussels (Latitude = 50.84673, Longitude = 4.35247) at zoom level 12.
This tutorial is based on [OpenLayers Quick Start Guide](https://openlayers.org/en/latest/doc/quickstart.html) and the [OpenLayers Popup Example](https://openlayers.org/en/latest/examples/popup.html) and uses version `5.3.0` of the library. I suggest you check if there is a more up-to-date version before proceeding.
86
88
@@ -92,21 +94,21 @@ The OpenLayers library is more powerful than the Leaflet library and offers more
4. Now you can add a `<script>` section at the end of the `<body>` section (after the `<script>` that loads OpenLayers JavaScript library). All the following JavaScript code needs to go in that new `<script>` section.
109
+
1. Now you can add a `<script>` section at the end of the `<body>` section (after the `<script>` that loads OpenLayers JavaScript library). All the following JavaScript code needs to go in that new `<script>` section.
108
110
109
-
5. Initialize the map with the OpenStreetMap Belgium baselayer
111
+
1. Initialize the map with the OpenStreetMap Belgium baselayer
110
112
111
113
```js
112
114
var attribution =newol.control.Attribution({
@@ -135,7 +137,7 @@ The OpenLayers library is more powerful than the Leaflet library and offers more
135
137
136
138
The map will be displayed in `<div id="map">` element and will be centered on Brussels (Latitude = 50.84673, Longitude = 4.35247) at zoom level 12.
137
139
138
-
6. You can add a marker at a specific location
140
+
1. You can add a marker at a specific location
139
141
140
142
```js
141
143
var layer =newol.layer.Vector({
@@ -150,7 +152,7 @@ The OpenLayers library is more powerful than the Leaflet library and offers more
150
152
map.addLayer(layer);
151
153
```
152
154
153
-
7. You can create a new `<div id="popup">` element right after the `<div id="map">` element
155
+
1. You can create a new `<div id="popup">` element right after the `<div id="map">` element
154
156
155
157
```html
156
158
<div id="popup"class="ol-popup">
@@ -159,7 +161,7 @@ The OpenLayers library is more powerful than the Leaflet library and offers more
159
161
</div>
160
162
```
161
163
162
-
8. Initialize the popup (the following JavaScript code needs to go in the `<script>` section)
164
+
1. Initialize the popup (the following JavaScript code needs to go in the `<script>` section)
163
165
164
166
```js
165
167
var container =document.getElementById('popup');
@@ -182,7 +184,7 @@ The OpenLayers library is more powerful than the Leaflet library and offers more
182
184
};
183
185
```
184
186
185
-
9. Add the function to open the popup when you click on the marker
187
+
1. Add the function to open the popup when you click on the marker
186
188
187
189
```js
188
190
map.on('singleclick', function (event) {
@@ -208,7 +210,7 @@ The OpenLayers library is more powerful than the Leaflet library and offers more
208
210
209
211
Now, you can check [the result](/openlayers-demo.html)!
0 commit comments