【问题标题】:Updating singleton value not reflected in AGM Map更新未反映在 AGM 地图中的单例值
【发布时间】: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


    【解决方案1】:

    好的,所以“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();    
    
    });
    

    ...一切都很开心!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2020-09-20
      相关资源
      最近更新 更多