I import <script async defer src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&key=<API_KEY>"></script>
at the end of index.html
this code works (I removed some parts from it but kept the relevant ones)
import { Component, ViewChild, ElementRef } from '@angular/core'
import { Platform } from 'ionic-angular';
import { Geoposition } from '@ionic-native/geolocation';
import { mapStyle } from "../../shared/map/map.style";
import { MapService } from '../../shared/map/map.service';
import { CustomMarker } from '../../shared/map/custom.marker';
@Component({
selector: 'map',
templateUrl: 'map.html'
})
export class MapPage {
@ViewChild('mapCanvas') mapElement: ElementRef;
map: any;
markers: any[] = [];
constructor(private platform: Platform,
private mapService: MapService) {
this.loadMap();
}
loadMap() {
this.mapService.getCurrentPosition().then((position: Geoposition) => {
this.mapService.position = position;
let mapEle = this.mapElement.nativeElement;
let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
let mapOptions = {
center: latLng,
zoom: 15,
styles: mapStyle,
disableDefaultUI: true,
scaleControl: false,
scrollwheel: false,
navigationControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map(mapEle, mapOptions);
this.addMarker({ name: 'My Location', lat: position.coords.latitude, lng: position.coords.longitude, draggable: true });
}, (error) => {
console.log(`Error getting current location: ${error}`);
});
}
// Adds a marker to the map and push to the array.
addMarker(markerData: any) {
let infoWindow = new google.maps.InfoWindow({
content: `<span>${markerData.name}</span>`
});
let marker = new google.maps.Marker({
position: markerData,
map: this.map,
title: markerData.name,
draggable: markerData.draggable
});
// let marker = new CustomMarker(
// new google.maps.LatLng(markerData.lat, markerData.lng),
// this.map,
// {
// img: 'http://placekitten.com/90/90'
// }
// );
this.markers.push(marker);
}
}
but when I replace default google maps marker with custom marker, you can look at the addMarker(), then I get error: ReferenceError: google is not defined in CustomMarker class