Skip to content

Commit efef976

Browse files
Merge pull request #100 from Sybit-Education/fix/map
fix map icons not loading
2 parents 836a386 + 719f19e commit efef976

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/app/components/detail/detail.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h1 class="card-title text-2xl font-bold text-gray-800">
1414
<button
1515
(click)="onBookmark(activity.osm_id)"
1616
class="bg-[#6bc4f6] hover:bg-[#1891d7] active:bg-[#6bc4f6] transition-all ease-in duration-75 min-w-10 min-h-10 max-h-10 max-w-10 w-10 h-10 flex justify-center items-center text-white rounded-md">
17-
@if (isBookmarked) {
17+
@if (getBookmarked(activity.osm_id)) {
1818
<i id="bookmark_id_2" class="bi bi-bookmark-fill"
1919
></i>
2020
} @else {

src/app/components/detail/detail.component.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,39 @@ export class DetailComponent implements OnInit {
4343
}
4444

4545
onBookmark(osm_id: number | null | undefined) {
46-
this.isBookmarked = !this.isBookmarked
47-
const item = localStorage.getItem("savedLocations")
46+
if (osm_id === null || osm_id === undefined) {
47+
return;
48+
}
49+
50+
this.isBookmarked = !this.isBookmarked;
51+
const item = localStorage.getItem("savedLocations");
4852
if (item) {
49-
const savedLocations = JSON.parse(item)
53+
const savedLocations = JSON.parse(item);
54+
const index = savedLocations.indexOf(osm_id);
55+
5056
if (this.isBookmarked) {
51-
savedLocations.push(osm_id)
57+
if (index === -1) {
58+
savedLocations.push(osm_id);
59+
}
5260
} else {
53-
const index = savedLocations.indexOf(osm_id)
54-
savedLocations.splice(index, 1)
61+
if (index !== -1) {
62+
savedLocations.splice(index, 1);
63+
}
5564
}
56-
localStorage.setItem("savedLocations", JSON.stringify(savedLocations))
65+
66+
localStorage.setItem("savedLocations", JSON.stringify(savedLocations));
5767
} else {
58-
localStorage.setItem("savedLocations", JSON.stringify([osm_id]))
68+
localStorage.setItem("savedLocations", JSON.stringify([osm_id]));
5969
}
60-
6170
}
6271

6372
getBookmarked(osm_id: number | null | undefined) {
6473
const item = localStorage.getItem("savedLocations")
6574
if(item) {
6675
const savedLocations = JSON.parse(item)
6776
this.isBookmarked = savedLocations.includes(osm_id)
77+
return this.isBookmarked
6878
}
79+
return false
6980
}
7081
}

src/app/components/map/map.component.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Router } from '@angular/router';
1010
import { AirtableService } from '../../services/airtable.service';
1111
import { MapBrowserEvent } from 'ol';
1212
import { NgStyle } from "@angular/common";
13+
import { Activity } from '../../types/activity.interface';
1314

1415
@Component({
1516
selector: 'app-map',
@@ -38,6 +39,10 @@ export class MapComponent implements OnInit {
3839
}
3940
return false;
4041
}
42+
43+
getColor(activity: Activity) {
44+
return this.getBookmarked(activity.osm_id) ? "gold" : activity.type.color
45+
}
4146

4247
ngOnInit(): void {
4348
this.vectorSource = new VectorSource();
@@ -67,21 +72,31 @@ export class MapComponent implements OnInit {
6772
// INITIALIZE
6873

6974
this.airtableService.getActivityList().subscribe(activities => {
70-
activities.forEach(activity => {
75+
// Ensure features are added to the vector source and the map is refreshed
76+
activities.forEach((activity) => {
7177
const feature = new Feature({
7278
geometry: new Point(fromLonLat([activity.longitude, activity.latitude])),
73-
activity: activity
79+
activity: activity,
80+
style: new Style({
81+
image: new Icon({
82+
src: 'data:image/svg+xml;utf8,' + activity.type.svg,
83+
color: this.getColor(activity),
84+
size: [this.iconSize, this.iconSize]
85+
})
86+
})
7487
});
75-
88+
this.vectorSource.addFeature(feature);
89+
});
90+
this.tooltip.nativeElement.style.display = 'none';
91+
this.vectorSource.getFeatures().forEach((feature: Feature<Geometry>) => {
92+
const activity = feature.get('activity');
7693
feature.setStyle(new Style({
7794
image: new Icon({
7895
src: 'data:image/svg+xml;utf8,' + activity.type.svg,
79-
color: activity.type.color,
80-
size: [this.iconSize, this.iconSize]
96+
color: this.getColor(activity),
97+
scale: this.iconSize
8198
})
8299
}));
83-
84-
this.vectorSource.addFeature(feature);
85100
});
86101
});
87102
}
@@ -115,7 +130,7 @@ export class MapComponent implements OnInit {
115130
(feature as Feature<Geometry>).setStyle(new Style({
116131
image: new Icon({
117132
src: 'data:image/svg+xml;utf8,' + activity.type.svg,
118-
color: activity.type.color,
133+
color: this.getColor(activity),
119134
scale: this.iconSize,
120135
})
121136
}));
@@ -131,7 +146,7 @@ export class MapComponent implements OnInit {
131146
feature.setStyle(new Style({
132147
image: new Icon({
133148
src: 'data:image/svg+xml;utf8,' + activity.type.svg,
134-
color: activity.type.color,
149+
color: this.getColor(activity),
135150
scale: this.iconSize
136151
})
137152
}));

0 commit comments

Comments
 (0)