How to assign value of lat and lng on ngoninit event ?

8 views
Skip to first unread message

ahmed elbarbary

unread,
Dec 27, 2019, 1:50:34 PM12/27/19
to Angular and AngularJS discussion
I need to assign value of lat and lng on event ngOnInit
how to do that please 
i assign value 
this.lat
this.lng
on  ngoninit event but map not display
so How to assign values of lng and lat on event ngoninit and display map
based on vlues assigned on ngoninit
import { Component, AfterViewInit, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector
: 'app-root',
  templateUrl
: './app.component.html',
  styleUrls
: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  title
= 'angular-gmap';
 
@ViewChild('mapContainer', { static: false }) gmap: ElementRef;
  map
: google.maps.Map;
  lat
= 40.73061;
  lng
= -73.935242;
 


  coordinates
= new google.maps.LatLng(this.lat, this.lng);

  mapOptions
: google.maps.MapOptions = {
    center
: this.coordinates,
    zoom
: 8
 
};

  marker
= new google.maps.Marker({
    position
: this.coordinates,
    map
: this.map,
    title
: 'Hello World!'
 
});
  ngOnInit
() {
   
//here i need to assign values of lat and lng and display map based on these values
    console
.log("Hi man");
   
 
}
  ngAfterViewInit
() {
    console
.log("Hi women");
   
this.mapInitializer();
 
}

  mapInitializer
() {
   
this.map = new google.maps.Map(this.gmap.nativeElement, this.mapOptions);
   
this.marker.setMap(this.map);
 
}
}


Reply all
Reply to author
Forward
0 new messages