Browse Source

Fix load Google Map

pull/2835/head
Vladyslav_Prykhodko 6 years ago
parent
commit
8f880da13d
  1. 2
      ui-ngx/src/app/core/services/resources.service.ts
  2. 1
      ui-ngx/src/app/modules/home/components/widget/lib/maps/map-models.ts
  3. 2
      ui-ngx/src/app/modules/home/components/widget/lib/maps/map-widget2.ts
  4. 60
      ui-ngx/src/app/modules/home/components/widget/lib/maps/providers/google-map.ts

2
ui-ngx/src/app/core/services/resources.service.ts

@ -48,7 +48,7 @@ export class ResourcesService {
}
let fileType;
const match = /[.](css|less|html|htm|js)?((\?|#).*)?$/.exec(url);
const match = /[./](css|less|html|htm|js)?(([?#]).*)?$/.exec(url);
if (match !== null) {
fileType = match[1];
}

1
ui-ngx/src/app/modules/home/components/widget/lib/maps/map-models.ts

@ -48,6 +48,7 @@ export type MapSettings = {
mapImageUrl?: string;
provider?: MapProviders;
credentials?: any; // declare credentials format
gmApiKey?: string;
defaultCenterPosition?: LatLngTuple;
markerClusteringSetting?;
useDefaultCenterPosition?: boolean;

2
ui-ngx/src/app/modules/home/components/widget/lib/maps/map-widget2.ts

@ -76,7 +76,7 @@ export class MapWidgetController implements MapWidgetInterface {
return;
}
parseWithTranslation.setTranslate(this.translate);
this.map = new MapClass($element, this.settings);
this.map = new MapClass($element, this.settings, this.ctx.$injector);
this.map.setImageAlias(this.subscribeForImageAttribute());
this.map.saveMarkerLocation = this.setMarkerLocation;
}

60
ui-ngx/src/app/modules/home/components/widget/lib/maps/providers/google-map.ts

@ -19,39 +19,45 @@ import L from 'leaflet';
import LeafletMap from '../leaflet-map';
import { UnitedMapSettings } from '../map-models';
import 'leaflet.gridlayer.googlemutant';
import { ResourcesService } from '@core/services/resources.service';
import { Injector } from '@angular/core';
let googleLoaded = false;
const gmGlobals: GmGlobal = {};
interface GmGlobal {
[key: string]: boolean;
}
export class GoogleMap extends LeafletMap {
constructor($container, options: UnitedMapSettings) {
private resource: ResourcesService;
super($container, options);
this.loadGoogle(() => {
const map = L.map($container).setView(options?.defaultCenterPosition, options?.defaultZoomLevel);
(L.gridLayer as any).googleMutant({
type: options?.gmDefaultMapType || 'roadmap'
}).addTo(map);
super.setMap(map);
}, options.credentials.apiKey);
super.initSettings(options);
}
constructor($container, options: UnitedMapSettings, private injector: Injector) {
super($container, options);
this.resource = injector.get(ResourcesService);
this.loadGoogle(() => {
const map = L.map($container, {attributionControl: false}).setView(options?.defaultCenterPosition, options?.defaultZoomLevel);
(L.gridLayer as any).googleMutant({
type: options?.gmDefaultMapType || 'roadmap'
}).addTo(map);
super.setMap(map);
}, options.gmApiKey);
super.initSettings(options);
}
private loadGoogle(callback, apiKey = 'AIzaSyDoEx2kaGz3PxwbI9T7ccTSg5xjdw8Nw8Q') {
if (googleLoaded) {
callback()
}
else {
googleLoaded = true;
const script = document.createElement('script');
script.onload = () => {
callback();
}
script.onerror = () => {
googleLoaded = false;
}
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}`;
document.getElementsByTagName('head')[0].appendChild(script);
private loadGoogle(callback, apiKey = 'AIzaSyDoEx2kaGz3PxwbI9T7ccTSg5xjdw8Nw8Q') {
if (gmGlobals[apiKey]) {
callback()
} else {
this.resource.loadResource(`https://maps.googleapis.com/maps/api/js?key=${apiKey}`).subscribe(
() => {
gmGlobals[apiKey] = true;
callback();
},
(error) => {
gmGlobals[apiKey] = false;
console.error(`Google map api load failed!`, error);
}
);
}
}
}

Loading…
Cancel
Save