<commit_before>---
title: Quick Start
layout: doc.hbs
---

# Quick Start

This primer shows you how to put a simple map on a web page.

**For production, we strongly recommend bundling the application together with its dependencies, as explained in the [Building an OpenLayers Application](./tutorials/bundle.html) tutorial.**


## Put a map on a page

Below you'll find a complete working example.  Create a new file, copy in the contents below, and open in a browser:

```xml
<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/{{ latest }}/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/{{ latest }}/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <h2>My Map</h2>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([37.41, 8.82]),
          zoom: 4
        })
      });
    </script>
  </body>
</html>
```

## Understanding what is going on

To include a map a web page you will need 3 things:

 1. Include OpenLayers
 2. `<div>` map container
 3. JavaScript to create a simple map

### Include OpenLayers

```xml
  <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/{{ latest }}/build/ol.js"></script>
```

The first part is to include the JavaScript library. For the purpose of this tutorial, here we simply point to the openlayers.org website to get the whole library. In a production environment, we would build a custom version of the library including only the module needed for our application.

**Optional:** If the application is intended to run on old platforms like Internet Explorer or Android 4.x, another script needs to be included before OpenLayers:

```xml
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList"></script>
```


### `<div>` to contain the map

```xml
  <div id="map" class="map"></div>
```

The map in the application is contained in a [`<div>` HTML element](http://en.wikipedia.org/wiki/Span_and_div). Through this `<div>` the map properties like width, height and border can be controlled through CSS. Here's the CSS element used to make the map 400 pixels high and as wide as the browser window.

```xml
  <style>
    .map {
      height: 400px;
      width: 100%;
    }
  </style>
```

### JavaScript to create a simple map

```js
  var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([37.41, 8.82]),
      zoom: 4
    })
  });
```

With this JavaScript code, a map object is created with an OSM layer zoomed on the African East coast. Let's break this down:

The following line creates an OpenLayers `Map` object. Just by itself, this does nothing since there's no layers or interaction attached to it.

```js
  var map = new ol.Map({ ... });
```

To attach the map object to the `<div>`, the map object takes a `target` into arguments. The value is the `id` of the `<div>`:

```js
    target: 'map'
```

The `layers: [ ... ]` array is used to define the list of layers available in the map. The first and only layer right now is a tiled layer:

```js
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ]
```

Layers in OpenLayers are defined with a type (Image, Tile or Vector) which contains a source. The source is the protocol used to get the map tiles. You can consult the list of [available layer sources here](/en/{{ latest }}/apidoc/ol.source.html)

The next part of the `Map` object is the `View`. The view allows to specify the center, resolution, and rotation of the map. The simplest way to define a view is to define a center point and a zoom level.  Note that zoom level 0 is zoomed out.

```js
    view: new ol.View({
      center: ol.proj.fromLonLat([37.41, 8.82]),
      zoom: 4
    })
```

You will notice that the `center` specified is in lon/lat coordinates (EPSG:4326). Since the only layer we use is in Spherical Mercator projection (EPSG:3857), we can reproject them on the fly to be able to zoom the map on the right coordinates.<commit_msg>Remove broken link to sources<commit_after>---
title: Quick Start
layout: doc.hbs
---

# Quick Start

This primer shows you how to put a simple map on a web page.

**For production, we strongly recommend bundling the application together with its dependencies, as explained in the [Building an OpenLayers Application](./tutorials/bundle.html) tutorial.**


## Put a map on a page

Below you'll find a complete working example.  Create a new file, copy in the contents below, and open in a browser:

```xml
<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/{{ latest }}/css/ol.css" type="text/css">
    <style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/{{ latest }}/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <h2>My Map</h2>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([37.41, 8.82]),
          zoom: 4
        })
      });
    </script>
  </body>
</html>
```

## Understanding what is going on

To include a map a web page you will need 3 things:

 1. Include OpenLayers
 2. `<div>` map container
 3. JavaScript to create a simple map

### Include OpenLayers

```xml
  <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/{{ latest }}/build/ol.js"></script>
```

The first part is to include the JavaScript library. For the purpose of this tutorial, here we simply point to the openlayers.org website to get the whole library. In a production environment, we would build a custom version of the library including only the module needed for our application.

**Optional:** If the application is intended to run on old platforms like Internet Explorer or Android 4.x, another script needs to be included before OpenLayers:

```xml
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList"></script>
```


### `<div>` to contain the map

```xml
  <div id="map" class="map"></div>
```

The map in the application is contained in a [`<div>` HTML element](http://en.wikipedia.org/wiki/Span_and_div). Through this `<div>` the map properties like width, height and border can be controlled through CSS. Here's the CSS element used to make the map 400 pixels high and as wide as the browser window.

```xml
  <style>
    .map {
      height: 400px;
      width: 100%;
    }
  </style>
```

### JavaScript to create a simple map

```js
  var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([37.41, 8.82]),
      zoom: 4
    })
  });
```

With this JavaScript code, a map object is created with an OSM layer zoomed on the African East coast. Let's break this down:

The following line creates an OpenLayers `Map` object. Just by itself, this does nothing since there's no layers or interaction attached to it.

```js
  var map = new ol.Map({ ... });
```

To attach the map object to the `<div>`, the map object takes a `target` into arguments. The value is the `id` of the `<div>`:

```js
    target: 'map'
```

The `layers: [ ... ]` array is used to define the list of layers available in the map. The first and only layer right now is a tiled layer:

```js
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ]
```

Layers in OpenLayers are defined with a type (Image, Tile or Vector) which contains a source. The source is the protocol used to get the map tiles.

The next part of the `Map` object is the `View`. The view allows to specify the center, resolution, and rotation of the map. The simplest way to define a view is to define a center point and a zoom level.  Note that zoom level 0 is zoomed out.

```js
    view: new ol.View({
      center: ol.proj.fromLonLat([37.41, 8.82]),
      zoom: 4
    })
```

You will notice that the `center` specified is in lon/lat coordinates (EPSG:4326). Since the only layer we use is in Spherical Mercator projection (EPSG:3857), we can reproject them on the fly to be able to zoom the map on the right coordinates.
{'fraction_non_alphanumeric': 0.1214779081241092, 'fraction_numerical': 0.012279355333844973, 'mean_word_length': 3.0452328159645234, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2802839', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2711, 'n_words': 1155}