# Generating a OpenLayers map as a Component

**URL:** <https://forum.ionicframework.com/t/generating-a-openlayers-map-as-a-component/161373>\
**Category:** Ionic Framework\
**Created:** [April 11, 2019, 12:31pm UTC](https://forum.ionicframework.com/t/generating-a-openlayers-map-as-a-component/161373 "2019-04-11T12:31:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonardofmed](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@leonardofmed](https://forum.ionicframework.com/u/leonardofmed)\
**Post date:** [April 11, 2019, 12:31pm UTC](https://forum.ionicframework.com/t/generating-a-openlayers-map-as-a-component/161373/1 "2019-04-11T12:31:13Z")

</div>

**Ionic v4 & OpenLayers v5.3**

I’m trying to generate a OL map as a component, but the only thing that is loaded is the icons. It seems that the styles and the JS configuration is not loading correctly, but I can’t figure out why. I was able to generate the map injecting the functions inside the main page configuration file (home.page.ts), but the quality of the map is low and blurry.

While trying to fix this I found some people suggesting to load the map as Component, so here I am. The last time I used Ionic he had just switched to version 3, so I’m a little rusty and might be missing something.

**The open-layers.component.ts code:**

```auto
import { Component, OnInit, ElementRef, ViewChild, Renderer } from '@angular/core';
import { Map } from 'ol';
import { OSM } from 'ol/source.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';

@Component({
  selector: 'app-open-layers',
  templateUrl: './open-layers.component.html',
  styleUrls: ['./open-layers.component.scss'],
})

export class OpenLayersComponent implements OnInit {
	@ViewChild('mapRef') mapRef: ElementRef;

	constructor() {}

	ngOnInit() {

		console.log('Hello');
	
  		var map = new Map({
  		  	layers: [
  		  		new TileLayer({ 
  		  			source: new OSM()
  		  		})],
  		  	target: document.getElementById('map'),	
  		  	view: new View({
  		  	  center: [0, 0],
  		  	  zoom: 3
  		  	})
  		});
	}
}

```

**Inside open-layers.component.html:**

`<div #mapRef id="map" class="map"></div>`

**home.page.html:**

```auto
<ion-content>
  <app-open-layers></app-open-layers>
</ion-content>

```

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [April 11, 2019, 11:57pm UTC](https://forum.ionicframework.com/t/generating-a-openlayers-map-as-a-component/161373/2 "2019-04-11T23:57:43Z")

</div>

See:

- [Why AfterViewInit conference app map](https://forum.ionicframework.com/t/why-afterviewinit-conference-app-map/157100/2)
- [Use OpenLayers 4 with Angular 5](https://stackoverflow.com/questions/48283679/use-openlayers-4-with-angular-5)
- [Cannot get the openlayers css in angular](https://stackoverflow.com/questions/51602298/cannot-get-the-openlayers-css-in-angular)

---

<div class="post-metadata">

**Author:** ![ishaquemujtaba](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ishaquemujtaba/32/125667_2.png) [@ishaquemujtaba](https://forum.ionicframework.com/u/ishaquemujtaba)\
**Post date:** [April 12, 2019, 5:42am UTC](https://forum.ionicframework.com/t/generating-a-openlayers-map-as-a-component/161373/3 "2019-04-12T05:42:26Z")

</div>

It will be great if you follow from here

[https://angular.io/tutorial/toh-pt4](https://angular.io/tutorial/toh-pt4)

---

<div class="post-metadata">

**Author:** ![leonardofmed](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@leonardofmed](https://forum.ionicframework.com/u/leonardofmed)\
**Post date:** [April 15, 2019, 1:28pm UTC](https://forum.ionicframework.com/t/generating-a-openlayers-map-as-a-component/161373/4 "2019-04-15T13:28:13Z")

</div>

I was able to generate the map as a component thanks to the @robinyo links. What I needed to change was:

Define map variable as the OL Map class in the beggining of the code and load the map inside `ngOnInit()`. This also loads the styles buttons that don’t load in previous example. I still need to update the size of the map to show him though. The blurry screen in mobile appear to be a device thing, if I try to open the OL online example page it is also blurry.

**openlayers.component.ts**

```auto
import { Component, OnInit, Renderer, ViewChild } from '@angular/core';
import { NavController, Platform } from '@ionic/angular';
import { Map } from 'ol';
import { OSM, Vector as VectorSource } from 'ol/source.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';

@Component({
  selector: 'app-openlayers',
  templateUrl: './openlayers.component.html',
  styleUrls: ['./openlayers.component.scss'],
})
export class OpenlayersComponent implements OnInit {

	map: Map;
	
  	constructor() { }
	
  	ngOnInit() {
  		this.map = new Map({
		  layers: [
		  	new TileLayer({ 
		  		source: new OSM()
		  	})],
		  target: document.getElementById('map'),
		  view: new View({
		    center: [0, 0],
		    zoom: 3
		  })
		});
		setTimeout(() => {
  	  	  this.map.updateSize();
  	  	}, 500);
  	}
}

```

**home.page.html**

```auto
<ion-header>
  	<ion-toolbar>
  	  	<ion-title>
  	  	  	Ionic Blank
  	  	</ion-title>
  	</ion-toolbar>
  	<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css">
	<style>html, body { margin: 0; }</style>
</ion-header>

<ion-content>
	<app-openlayers></app-openlayers>
</ion-content>

```

**openlayers.component.html**

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