我们已经得到了生产现场进行运行奥里利亚嵌入谷歌地图(https://farmtracksa.com)和目前正在使用我们自己的组件来管理在页面中嵌入Google地图(并通过DI将地图展示给我们的其他页面和组件)。
我们的网站是写了在打字稿,我们的谷歌地图的控制被分解成服务和组件,如下所示:
import { autoinject } from "aurelia-framework";
@autoinject
export class GoogleMaps {
constructor() {
this.initialized = new Promise<void>((resolve, reject) => {
this.onInitialized = resolve;
this.onInitializationFailed = reject;
});
}
map: google.maps.Map = null;
initialized: PromiseLike<void>;
private onInitialized:() => void;
private onInitializationFailed: (err: Error) => void;
initialize(element: Element) {
this.map = new google.maps.Map(element, {});
this.onInitialized();
}
}
与控制,您可以在您的视图嵌入。
import {bindable, autoinject, inlineView, bindingMode, customElement,
inject} from "aurelia-framework";
import {DOM} from "aurelia-pal";
@inlineView("<template></template>")
@bindable({
name: "zoom",
changeHandler: "onZoomChanged",
defaultBindingMode: bindingMode.twoWay,
defaultValue: 17
})
@bindable({
name: "latitude",
changeHandler: "onCenterChanged",
defaultBindingMode: bindingMode.twoWay,
defaultValue: -34.1996316
})
@bindable({
name: "longitude",
changeHandler: "onCenterChanged",
defaultBindingMode: bindingMode.twoWay,
defaultValue: 19.0268722
})
@customElement("google-maps")
@inject(DOM.Element, GoogleMaps)
export class GoogleMapsControl {
static map: google.maps.Map = null;
constructor(private mapElement: Element, public googleMaps: GoogleMaps) {
}
bind() {
}
attached() {
this.googleMaps.initialize(this.mapElement);
this.googleMaps.map.setCenter(new google.maps.LatLng(this.latitude, this.longitude));
this.googleMaps.map.setZoom(this.zoom);
this.onCenterChanged();
}
changing = false;
zoom = 17;
latitude = -34.1996316;
longitude = 19.0268722;
onZoomChanged() {
this.googleMaps.map && this.googleMaps.map.setZoom(this.zoom);
}
onCenterChanged() {
this.googleMaps.map && this.latitude && this.longitude && this.googleMaps.map.setCenter({ lat: this.latitude, lng: this.longitude });
}
}
你会然后把它嵌入在像这样的观点:
<template>
<require from="./google-maps"></require>
<google-maps zoom="7"></google-maps>
</template>
最后,你还需要确保你加载谷歌地图API到你的Web应用程序。我通过在我的index.html
文件中放置以下片段(来自Google Maps Docs)来完成此操作,因为我在整个应用程序中使用了地图。
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
你要确保你尝试之前,你的页面上初始化GoogleMaps
元素,不这样做将导致您的开发者控制台中的错误谷歌地图脚本已被加载。
这个问题是一个很长的时间以前,我真的不记得了,并没有项目了。问一个新的问题,也许有人会有你的答案。 – nicovank
好的,谢谢你花时间回复。 –