I am creating a pretty basic web app as of right now. Basically, I just get the lat and long from my mongodb and put markers on the map, which is working fine. The problem I am having is setting the marker colors. I can make it work if I put the url to the marker png in the mongodb, but I was wondering if there is a way to change the marker color based on certain data from mongodb. Right now, I am just using a field called 'marker' in my mongodb document with a number 1 - 4, based on that number, will determine what color the marker is.
Here is the component.ts
import { Component, OnInit } from '@angular/core';
import { SchoolService } from '../school.service';
import { School } from '../School';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
startLat = 35.782169;
startLng = -80.793457;
zoom = 7;
greenMarker =
'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png';
yellowMarker =
'http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png';
schools: School[] = [];
constructor(private schoolService: SchoolService) { }
ngOnInit() {
this.getSchools();
this.getIcon();
}
getSchools(): void {
this.schoolService.getSchools()
.subscribe((schoolList: School[]) => {
this.schools = schoolList;
console.log(this.schools);
});
}
getIcon() {
for (let i = 0; i <= this.schools.length; i++) {
if (this.schools[i].marker === 1) {
return this.greenMarker;
} else {
return this.yellowMarker;
}
}
}
}
Here is the html
<div class="container">
<ul class="legend">
<li><span class="faster"></span>IPv6 Load Time ≤ IPv4</li>
<li><span class="all-elements"></span>Fully IPv6 Accessible</li>
<li><span class="reachable"></span>DNS AAA Record</li>
<li><span class="DNS-AAAA"></span>Not IPv6 Accessible</li>
</ul>
<agm-map id="map" [latitude]="startLat" [zoom]="zoom"
[longitude]="startLng">
<agm-marker *ngFor="let school of schools; let i = index"
[latitude]="school.lat" [longitude]="school.long"
[iconUrl]="getIcon()">
<agm-info-window>
<h4>{{ school.name }}</h4>
<p>lat: {{ school.lat }}</p>
<p>long: {{ school.long }}</p>
</agm-info-window>
</agm-marker>
</agm-map>
</div>
School.ts
export class School {
name: string;
lat: number;
long: number;
marker: number;
}
schoolService.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class SchoolService {
constructor(private http: HttpClient) { }
getSchools() {
return this.http.get('http://localhost:3000/get');
}
}
Here is the current log... MapComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'marker' of undefined at MapComponent.push../src/app/map/map.component.ts.MapComponent.getIcon (map.component.ts:38) at MapComponent.push../src/app/map/map.component.ts.MapComponent.ngOnInit (map.component.ts:25) at checkAndUpdateDirectiveInline (core.js:22099) at checkAndUpdateNodeInline (core.js:23363) at checkAndUpdateNode (core.js:23325) at debugCheckAndUpdateNode (core.js:23959) at debugCheckDirectivesFn (core.js:23919) at Object.eval [as updateDirectives] (MapComponent_Host.ngfactory.js? [sm]:1)