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

时间:2019-02-20 17:12:40

标签: angular openlayers-3 angular7 angular-google-maps openlayers-5

我正在尝试以编程方式设置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” 不知道为什么会在我声明的地方。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,所以'change:resolution'中的“ this.currZoom”未定义这一事实给了我一个线索...

我所做的是..

定义我的单身人士...

 Gate::define('viewNova', function ($user) {
     return in_array($user->email, [
         'my@license.com',
     ]);
 });

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

formValues = msFormValues;

然后我刚刚在'change:resolution'中更新了单例值

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

      </agm-map>

...一切都开心!