3

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?

3
  • 1
    It has already been answered -> stackoverflow.com/questions/37081943/… Commented Oct 10, 2017 at 13:59
  • Thank you for that link @mutantkeyboard . I updated my Question above. How do i access classes on the same object from another script? Commented Oct 11, 2017 at 8:29
  • If you're using the Angular CLI, use the scripts: stackoverflow.com/questions/38855891/… Commented Oct 11, 2017 at 8:45

3 Answers 3

2

You can add js in particular component like

ngOnInit(){
  var scriptUrl = 'http://js.api.here.com/v3/3.0/mapsjs-core.js';
  let node = document.createElement('script');
  node.src = scriptUrl;
  node.type = 'text/javascript';
  node.async = true;
  node.charset = 'utf-8';
  document.getElementsByTagName('head')[0].appendChild(node);
}

Sign up to request clarification or add additional context in comments.

1 Comment

can you exploian what node ,node.src,node.type,node.aysnce.node.charset is.
2

Ill answer my own question:

Adding the script hyperlinks to the index.html was the right approach. I was missing a TypeDefinition translating the Javascript Code to TypeScript.

https://www.npmjs.com/package/@types/heremaps

The link above shows where to get those. It can be installed via:

npm install --save @types/heremaps

The according files are installed into "node-modules/@types/..." As described in an answer to the following question:

Angular2: import external js file into component

I also needed to add:

declare var test: any;

to my component.

I am now able to access all the functions of all five external scripts. And my project looks kind of like this:

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">

  <link rel="stylesheet" href="http://js.api.here.com/v3/3.0/mapsjs-ui.css" />
  <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-ui.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>
  <script src="http://js.api.here.com/v3/3.0/mapsjs-pano.js" type="text/javascript" charset="utf-8"></script>

</head>
<body>
  <app-root></app-root>
</body>
</html>

app.component.ts

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
    });

    document.body.style.backgroundColor = 'black';

    // 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 }
      }
    );
    // document.getElementById('whiteSpace').style.backgroundColor = 'white';
    // Enable the event system on the map instance:
    var mapEvents = new H.mapevents.MapEvents(map);
    var ui = H.ui.UI.createDefault(map, defaultLayers);

    // Add event listeners:
    map.addEventListener('tap', function(evt) {
      // Log 'tap' and 'mouse' events:
      console.log(evt.type, evt.currentPointer.type);
    });

    // Instantiate the default behavior, providing the mapEvents object:
    var behavior = new H.mapevents.Behavior(mapEvents);
  }
}

Comments

0

the easiest way to import js file is to add it to an angular cli project (add it to scripts in .angular-cli-package.json), then simply : 1 - Add it to your main app module, as root(), with the right user + keys 2 - "import HereMap from 'heremaps' in the component not only 'declare var H: any;" as it does not indicate the lib path. You can do both in some cases, but with angular 4 I use import.

https://www.npmjs.com/package/angular-heremaps

6 Comments

Thank you for your answer. How do i achieve step 1.? Could you show me an example? I am fairly new to Angular and do not seem to understand all the core concepts.
The link you provided links to a npm package for angularjs. As far as i know its not possible to use angularjs code with angular 2/4.
It is possible of course, either js or angular js package, if you want to use it with typescript, then you can get the types. To achieve step 1 first get the package that seems to match your needs.
Sorry I pressed enter all the time, so here is the full answer : - If you are in an angular cli project add your css and js to apps in .angular-cli : [ scripts : [heremap.js, ...] and styles : [heremaps.css, ...] If not, add it to your main index.html file - If you can get the types do it as typescript is easier to write and read : npmjs.com/package/@types/heremaps - Then simply go in your component and "import * as HereMap from 'heremaps' " where heremaps is the declared name in the index.d.ts you just installed (npm install --save @types/heremaps)
I got it to work. You can see my code below. Yet i dont think its an elegant way. If i use "import * as HereMap from '@types/heremaps'" instead of "declare var H: any;" doesnt work though. Do you have any idea why? Where would i need to put the the scripts if i downloaded them? Do they go into the assets folder? Thank you for your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.