【发布时间】:2019-02-20 17:12:40
【问题描述】:
我正在尝试以编程方式设置 AGM 地图的缩放级别。
如果我...
HTML
<button (click)="changeZoom(2)">zoom</button>
<agm-map
[latitude]="lat"
[longitude]="lng"
[zoom]="currZoom"
[mapTypeId]="mapType"
[mapTypeControl]="mapControls"
[zoomControl]="mapControls"
[streetViewControl]="mapControls">
</agm-map>
组件代码
export class MsMapComponent implements OnInit {
lat: number = msFormValues.googleLat;
lng: number = msFormValues.googleLng;
currZoom: number = msFormValues.googleZoom;
mapType = 'satellite' ;
mapControls = false;
constructor() {
}
ngOnInit() {
const osmLayer = new TileLayer({
source: new OSM()
});
const xyzLayer = new TileLayer({
source: new XYZ({
url: 'http://tile.osm.org/{z}/{x}/{y}.png'
})
});
msFormValues.view = new View({
center: [0,0],
zoom: 0,
projection: 'EPSG:3857',
maxZoom: 20,
minZoom: 5
});
msFormValues.googleZoom = msFormValues.view.getZoom();
msFormValues.map = new olMap({
target: 'map',
layers: [
osmLayer,
// xyzLayer
],
view: msFormValues.view
});
msFormValues.view.on('change:center',function(){
var mapCenter = transform(msFormValues.view.getCenter(),'EPSG:3857',
'EPSG:4326');
msFormValues.googleLat = mapCenter[1];
msFormValues.googleLng = mapCenter[0];
});
msFormValues.view.on('change:resolution',function(){
msFormValues.googleZoom = msFormValues.view.getZoom();
this.currZoom = 2;
});
}
setMapType(mapTypeId: string) {}
changeZoom(zoomLeve: number){
this.currZoom = zoomLeve;
}
}
单例值
@Injectable()
export class msFormValues {
public static cropYear: any = '';
public static map: any = null;
public static view: any = null;
public static googleLat: any = 0;
public static googleLng: any = 0;
public static googleZoom: any = 5;
}
如果我通过单击按钮调用“changeZoom”并更新变量“currZoom”,AGM 地图会响应并更新缩放级别。 但是,如果我从“更改:分辨率”内部更新“currZoom”,则 AGM 地图不会更新……如果我尝试从“更改:分辨率”内部调用“changeZoom”,请注意“更改缩放”功能是“未定义。
更新 看起来“更改:分辨率”中的“this.currZoom”是“未定义” 不知道为什么我要声明它。
非常感谢任何帮助!
【问题讨论】:
标签: angular openlayers-3 angular7 angular-google-maps openlayers-5