I am trying to programmaticly set the Zoom level for an AGM map.
If I...
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>
Component code
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;
}
}
Singleton Values
@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;
}
If I call "changeZoom" with a button click and update the variable "currZoom" the AGM maps responds and updates the zoom level. However if I update "currZoom" from inside the 'change:resolution' watch the AGM map does not get updated...also if I try to call "changeZoom" from inside 'change:resolution' watch the "changeZoom" function is "undefined.
Update Looks like "this.currZoom" inside 'change:resolution' is "undefined" not sure why that would be where I'm declaring it.
Any help is greatly appreciated!!
Okay, so the fact that "this.currZoom" inside 'change:resolution' was undefined gave me a clue...
What I did was..
Define my singleton...
formValues = msFormValues;
Then pointed properties at the singleton variable I set up
<agm-map
[latitude]=(formValues.googleLat)
[longitude]=(formValues.googleLng)
[zoom]=(formValues.googleZoom)
[mapTypeId]="mapType"
[mapTypeControl]="mapControls"
[zoomControl]="mapControls"
[streetViewControl]="mapControls">
</agm-map>
Then I just updated the singleton value inside 'change:resolution'
msFormValues.view.on('change:resolution',function(){
msFormValues.googleZoom = msFormValues.view.getZoom();
});
...and everything is happy!