更新未反映在 AGM 地图中的单例值

Updating singleton value not reflected in AGM Map

我正在尝试以编程方式设置 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 地图会响应并更新缩放级别。 但是,如果我从 'change:resolution' 内部更新 "currZoom",请注意 AGM 地图不会更新...如果我尝试从 'change:resolution' 内部调用 "changeZoom",请注意 "changeZoom"函数是“未定义的。

更新 看起来 'change:resolution' 里面的 "this.currZoom" 是 "undefined" 不确定为什么我要声明它。

非常感谢任何帮助!!

好吧,所以 'change:resolution' 里面的 "this.currZoom" 是未定义的事实给了我一个线索...

我做的是..

定义我的单例...

formValues = msFormValues;

然后将属性指向我设置的单例变量

<agm-map  
        [latitude]=(formValues.googleLat)
        [longitude]=(formValues.googleLng)
        [zoom]=(formValues.googleZoom)
        [mapTypeId]="mapType" 
        [mapTypeControl]="mapControls" 
        [zoomControl]="mapControls" 
        [streetViewControl]="mapControls">

      </agm-map>

然后我就更新了里面的单例值'change:resolution'

msFormValues.view.on('change:resolution',function(){
  msFormValues.googleZoom = msFormValues.view.getZoom();    

});

...一切都很开心!