javascript - dynamically adding markers to google map - angular2 -
so have 2 components, first of them form use adding latitude , longitude service markers. main component map want add markers. first problem initialize map , data in ngoninit() can't dynamically add new data because component doesn't reload , oninit don't run again. second problem don't know how add new markers when initialize map in ngoninit.
that main component:
export class appcomponent implements oninit { parties: party[] = []; party_location: location[] = []; constructor(private partyservice: partyservice) { } ngoninit() { map = new google.maps.map(document.getelementbyid('map'), { center: { lat: -25.363, lng: 131.044 }, scrollwheel: true, zoom: 16 }); this.parties = this.partyservice.getdata(); //get data (let item of this.parties) { //try add markers let marker = new google.maps.marker({ map: map, position: { lat: item.lan1, lng: item.lan2 }, title: item.title }); } if (navigator.geolocation) { navigator.geolocation.getcurrentposition(function(position) { let pos = { lat: position.coords.latitude, lng: position.coords.longitude }; infowindow.setposition(pos); infowindow.setcontent('this you.'); infowindow.open(map); map.setcenter(pos); }); } let infowindow = new google.maps.infowindow; } }
may have advice how change code??
Comments
Post a Comment