Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: google is not defined on using PrimeNG GMap

I am using PrimeNg GMap in my project, but getting error "ReferenceError: google is not defined" i am following this link for documentation https://www.primefaces.org/primeng/#/gmap. and please tell me how to add api key for GMap. error image

My Component

import {GMapModule} from 'primeng/primeng';

@Component({
  selector: 'mycomponent',
  templateUrl: './mycomponent.component.html',

 [})
 export c][1]lass MyComponent  implements OnInit {

options: any;

overlays: any[];

ngOnInit() {
    this.options = {
        center: {lat: 36.890257, lng: 30.707417},
        zoom: 12
    };

    this.overlays = [
        new google.maps.Marker({position: {lat: 36.879466, lng: 30.667648}, title:"Konyaalti"}),
        new google.maps.Marker({position: {lat: 36.883707, lng: 30.689216}, title:"Ataturk Park"}),
        new google.maps.Marker({position: {lat: 36.885233, lng: 30.702323}, title:"Oldtown"}),
        new google.maps.Polygon({paths: [
            {lat: 36.9177, lng: 30.7854},{lat: 36.8851, lng: 30.7802},{lat: 36.8829, lng: 30.8111},{lat: 36.9177, lng: 30.8159}
        ], strokeOpacity: 0.5, strokeWeight: 1,fillColor: '#1976D2', fillOpacity: 0.35
        }),
        new google.maps.Circle({center: {lat: 36.90707, lng: 30.56533}, fillColor: '#1976D2', fillOpacity: 0.35, strokeWeight: 1, radius: 1500}),
        new google.maps.Polyline({path: [{lat: 36.86149, lng: 30.63743},{lat: 36.86341, lng: 30.72463}], geodesic: true, strokeColor: '#FF0000', strokeOpacity: 0.5, strokeWeight: 2})
    ];
}

Html code

<p-gmap [options]="options" [overlays]="overlays" [style]="{'width':'100%','height':'320px'}" ></p-gmap>
like image 521
Tariq Aqeel Avatar asked May 12 '17 07:05

Tariq Aqeel


3 Answers

The PrimeNG documentation isn't great on this bit, looking at the GitHub repo helped though. Try adding the google API Script to Index.html:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY_GOES_HERE"></script>

Note that you will need to add your own api key.

Also note that I removed the defer async as when loading a page directly this was causing an error.

like image 156
Simon Legg Avatar answered Nov 08 '22 01:11

Simon Legg


You need to download and use google.maps javascript library. See the documentation: https://developers.google.com/maps/documentation/javascript/tutorial

PrimeNG component is just a thin Angular wrapper for google map javascript library.

like image 5
Julia Passynkova Avatar answered Nov 07 '22 23:11

Julia Passynkova


Add this script in index.html file with valid google key.

<script src="https://maps.googleapis.com/maps/api/js?key="GOOGLE_MAP_KEY">
</script>
like image 2
Baljeet Singh Khalsa Avatar answered Nov 07 '22 23:11

Baljeet Singh Khalsa