i am currently trying to read myself into Angular 4. For a project i need to display a map using "HERE Maps" within an Angular App. But i can't figure out how to import the script while maintaining access to the classes within a component.
I am trying to follow the instructions from HERE: https://developer.here.com/documentation/maps/topics/quick-start.html
I tried adding the .js script to the index.html like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>See720</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="http://js.api.here.com/v3/3.0/mapsjs-core.js" type="text/javascript" charset="utf-8"></script>
<script src="http://js.api.here.com/v3/3.0/mapsjs-service.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var platform = new H.service.Platform({
'app_id': '****',
'app_code': '****',
useCIT: true
});
</script>
</head>
<body>
<app-root></app-root>
</body>
</html>
I tried to somehow inject the .js file into a component or a service in order to use the classes contained in the .js file. Yet i could not get it to work.
I expected to put this part:
var platform = new H.service.Platform({
'app_id': '****',
'app_code': '****',
useCIT: true
});
... into an OnInit() method somehow. But the "H" object never gets recognized. Using plane old JavaScript and Html i can get this to work though.
What is the correct way to import such a JavaScript file in Angular 4 and how do i access its classes and methods?
UPDATE
My code currently looks like this: Template:
<div style="text-align:center">
<!--<div><font color="white">This is some text!</font></div>-->
<div id="mapContainer" style="width: 900px; height: 600px"></div>
</div>
Component:
import {AfterViewInit, Component} from '@angular/core';
declare var H: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
title = 'C720';
ngAfterViewInit() {
// Initialize the platform object:
let platform = new H.service.Platform({
'app_id': '****',
'app_code': '****',
useCIT: true
});
// Obtain the default map types from the platform object
let defaultLayers = platform.createDefaultLayers();
// Instantiate (and display) a map object:
let map = new H.Map(
document.getElementById('mapContainer'),
defaultLayers.normal.map,
{
zoom: 5,
center: { lat: 52.5, lng: 13.5 }
}
);
}
}
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>See720</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="http://js.api.here.com/v3/3.0/mapsjs-core.js" type="text/javascript" charset="utf-8"></script>
<script src="http://js.api.here.com/v3/3.0/mapsjs-service.js" type="text/javascript" charset="utf-8"></script>
<script src="http://js.api.here.com/v3/3.0/mapsjs-mapevents.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<app-root></app-root>
</body>
</html>
The map is displaying, but now i need to add an eventlistener and but the H.mapevent class from the third package ("mapjs-mapevents.js") is not recognized.
How can i tell Angular to look within the third script for other classes?