0

I am trying to incorporate Javascript google maps into my Angular 8 project in order to add more functionality than AGM. I have created a typings.d.ts file and included my javascript in the angular.json but i am still getting the error: 'map' is declared but its value is never read. Here is my code...

component.html

  <body>
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?key={{apiKey}}&callback=initMap"
    async defer>
    </script>
    <script >
    </script>
  </body>
</html>

component.ts

import * as map from 'map';
import { Component, OnInit } from '@angular/core';
import { environment } from '../../environments/environment';

@Component({
  selector: 'app-google-maps',
  templateUrl: './google-maps.component.html',
  styleUrls: ['./google-maps.component.css']
})
export class GoogleMapsComponent implements OnInit {

  private apiKey = environment.googleMapsAPIkey;

  constructor() { }

  ngOnInit() {
  }

}

maps.js

var map;
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: -34.397, lng: 150.644},
    zoom: 8
  });
}

typings.d.ts

declare module Map {
  export interface Main {
    map;
  }
}
declare var Map:Map.Main;

4
  • It says that imported 'map' is not used. import * as map from 'map'; Either use the map or remove the import statement if not required. Commented Dec 2, 2019 at 4:52
  • When I reference it, it says it cannot find the module Commented Dec 2, 2019 at 4:57
  • Are you able to display google maps div in your browser in normal without adding typings.d.ts file? By just including external script? Commented Dec 2, 2019 at 18:23
  • @PALLAMOLLASAI that’s how I thought it should work too... I get no error when I do that though... pretty sure I referenced my apiKey correctly too in the sript src Commented Dec 2, 2019 at 18:40

1 Answer 1

1

Following code displated google maps in developer mode.

html

<div style="width: 1000px;height: 300px;" id="map"></div>

component.ts

constructor() { 

var dynamicScripts = ["https://maps.googleapis.com/maps/api/js?key="apiKey"];

  for (var i = 0; i < dynamicScripts.length; i++) {
    let node = document.createElement('script');
    node.src = dynamicScripts [i];
    node.type = 'text/javascript';
    node.async = true;
    node.charset = 'utf-8';
    document.getElementsByTagName('head')[0].appendChild(node);
  }
}
initMap() {
  var uluru = {lat: -25.363, lng: 131.044};
  var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: uluru
  });

 }
 ngOnInit() {
   setTimeout(()=>{ // 
     this.initMap();
   },2000);
 }

Note Instead of setTimeout we can use promies.then().

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

3 Comments

thank you for the help, is the reason I couldnt load it normally because it is dynamic?
I think so. I will try to search more on this and will let you know..
So far everything seems to be working well except the clusterer package

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.