Skip to content

Commit 0d64bc9

Browse files
authored
Open stop information on click in route modal, add button to open stop modal (#201)
* Open stop information on click in route modal, add button to open stop modal * i18n string * Make stop popup show on Google Maps * Pass stop time through
1 parent c45f8fc commit 0d64bc9

File tree

6 files changed

+69
-48
lines changed

6 files changed

+69
-48
lines changed

src/components/MapContainer.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import { onMount } from 'svelte';
1010
import { MapSource } from './../config/mapSource.js';
1111
12-
let apiKey = env.GOOGLE_MAPS_API_KEY;
12+
let apiKey = env.PUBLIC_OBA_GOOGLE_MAPS_API_KEY;
1313
let { handleStopMarkerSelect, mapProvider = $bindable(), ...restProps } = $props();
1414
1515
onMount(() => {
1616
if (PUBLIC_OBA_MAP_PROVIDER === MapSource.Google) {
17-
mapProvider = new GoogleMapProvider(apiKey);
17+
mapProvider = new GoogleMapProvider(apiKey, handleStopMarkerSelect);
1818
} else if (PUBLIC_OBA_MAP_PROVIDER === MapSource.OpenStreetMap) {
19-
mapProvider = new OpenStreetMapProvider(apiKey);
19+
mapProvider = new OpenStreetMapProvider(handleStopMarkerSelect);
2020
} else {
2121
console.error('Unknown map provider:');
2222
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<script>
2-
let { stopName, arrivalTime } = $props();
2+
import { t } from 'svelte-i18n';
3+
let { stopName, arrivalTime, handleStopMarkerSelect } = $props();
34
</script>
45

56
<div class="transform rounded-lg bg-white p-4 shadow-lg">
67
<h3 class="text-xl font-bold text-gray-900">{stopName}</h3>
7-
<p class="mt-2 flex items-center text-gray-700">
8+
<p class="my-4 flex items-center text-gray-700">
89
<span class="mr-2 rounded-md bg-purple-600 px-2 py-1 text-white">Arrival time:</span>
910
<span
1011
>{new Date(arrivalTime * 1000).toLocaleTimeString([], {
@@ -13,4 +14,13 @@
1314
})}</span
1415
>
1516
</p>
17+
<div class="mt-auto flex justify-center">
18+
<button
19+
type="button"
20+
class="inline-block rounded-lg border border-brand bg-brand px-3 py-1 text-sm font-medium text-white shadow-md transition duration-200 ease-in-out hover:bg-brand-secondary"
21+
onclick={handleStopMarkerSelect}
22+
>
23+
{$t('view_stop_information')}
24+
</button>
25+
</div>
1626
</div>

src/components/routes/RouteModal.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
function handleStopItemClick(stop) {
99
mapProvider.flyTo(stop.lat, stop.lon, 18);
10+
mapProvider.openStopMarker(stop);
1011
}
1112
1213
function title() {

src/lib/Provider/GoogleMapProvider.svelte.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import TripPlanPinMarker from '$components/trip-planner/tripPlanPinMarker.svelte
99
import { mount, unmount } from 'svelte';
1010

1111
export default class GoogleMapProvider {
12-
constructor(apiKey) {
12+
constructor(apiKey, handleStopMarkerSelect) {
1313
this.apiKey = apiKey;
1414
this.map = null;
1515
this.globalInfoWindow = null;
@@ -18,6 +18,7 @@ export default class GoogleMapProvider {
1818
this.stopMarkers = [];
1919
this.vehicleMarkers = [];
2020
this.markersMap = new Map();
21+
this.handleStopMarkerSelect = handleStopMarkerSelect;
2122
}
2223

2324
async initMap(element, options) {
@@ -122,33 +123,37 @@ export default class GoogleMapProvider {
122123

123124
this.stopsMap.set(stop.id, stop);
124125

125-
marker.addListener('click', () => {
126-
if (this.globalInfoWindow) {
127-
this.globalInfoWindow.close();
128-
}
126+
marker.addListener('click', () => this.openStopMarker(stop, stopTime));
129127

130-
if (this.popupContentComponent) {
131-
unmount(this.popupContentComponent);
132-
}
128+
this.markersMap.set(stop.id, marker);
129+
this.stopMarkers.push(marker);
130+
}
133131

134-
const popupContainer = document.createElement('div');
132+
openStopMarker(stop, stopTime = null) {
133+
if (this.globalInfoWindow) {
134+
this.globalInfoWindow.close();
135+
}
135136

136-
this.popupContentComponent = mount(PopupContent, {
137-
target: popupContainer,
138-
props: {
139-
stopName: stop.name,
140-
arrivalTime: stopTime ? stopTime.arrivalTime : null
141-
}
142-
});
137+
if (this.popupContentComponent) {
138+
unmount(this.popupContentComponent);
139+
}
143140

144-
this.globalInfoWindow = new google.maps.InfoWindow({
145-
content: popupContainer
146-
});
141+
const popupContainer = document.createElement('div');
142+
143+
this.popupContentComponent = mount(PopupContent, {
144+
target: popupContainer,
145+
props: {
146+
stopName: stop.name,
147+
arrivalTime: stopTime ? stopTime.arrivalTime : null,
148+
handleStopMarkerSelect: () => this.handleStopMarkerSelect(stop)
149+
}
150+
});
147151

148-
this.globalInfoWindow.open(this.map, marker);
152+
this.globalInfoWindow = new google.maps.InfoWindow({
153+
content: popupContainer
149154
});
150155

151-
this.stopMarkers.push(marker);
156+
this.globalInfoWindow.open(this.map, this.markersMap.get(stop.id));
152157
}
153158

154159
highlightMarker(stopId) {

src/lib/Provider/OpenStreetMapProvider.svelte.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import TripPlanPinMarker from '$components/trip-planner/tripPlanPinMarker.svelte
1111
import { mount, unmount } from 'svelte';
1212

1313
export default class OpenStreetMapProvider {
14-
constructor() {
14+
constructor(handleStopMarkerSelect) {
15+
this.handleStopMarkerSelect = handleStopMarkerSelect;
1516
this.map = null;
1617
this.L = null;
1718
this.globalInfoWindow = null;
@@ -153,32 +154,35 @@ export default class OpenStreetMapProvider {
153154

154155
this.stopsMap.set(stop.id, stop);
155156

156-
marker.on('click', () => {
157-
if (this.globalInfoWindow) {
158-
this.map.closePopup(this.globalInfoWindow);
159-
}
157+
marker.on('click', () => this.openStopMarker(stop, stopTime));
160158

161-
if (this.popupContentComponent) {
162-
unmount(this.popupContentComponent);
163-
}
159+
this.stopMarkers.push(marker);
160+
}
164161

165-
const popupContainer = document.createElement('div');
162+
openStopMarker(stop, stopTime = null) {
163+
if (this.globalInfoWindow) {
164+
this.map.closePopup(this.globalInfoWindow);
165+
}
166166

167-
this.popupContentComponent = mount(PopupContent, {
168-
target: popupContainer,
169-
props: {
170-
stopName: stop.name,
171-
arrivalTime: stopTime ? stopTime.arrivalTime : null
172-
}
173-
});
167+
if (this.popupContentComponent) {
168+
unmount(this.popupContentComponent);
169+
}
170+
171+
const popupContainer = document.createElement('div');
174172

175-
this.globalInfoWindow = L.popup()
176-
.setLatLng([stop.lat, stop.lon])
177-
.setContent(popupContainer)
178-
.openOn(this.map);
173+
this.popupContentComponent = mount(PopupContent, {
174+
target: popupContainer,
175+
props: {
176+
stopName: stop.name,
177+
arrivalTime: stopTime ? stopTime.arrivalTime : null,
178+
handleStopMarkerSelect: () => this.handleStopMarkerSelect(stop)
179+
}
179180
});
180181

181-
this.stopMarkers.push(marker);
182+
this.globalInfoWindow = L.popup()
183+
.setLatLng([stop.lat, stop.lon])
184+
.setContent(popupContainer)
185+
.openOn(this.map);
182186
}
183187

184188
removeStopMarkers() {

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@
108108
"page": "Page",
109109
"show": "Show",
110110
"hide": "Hide"
111-
}
111+
},
112+
"view_stop_information": "View Stop Information"
112113
}

0 commit comments

Comments
 (0)