Skip to content

Commit b553b57

Browse files
committed
Make stop popup show on Google Maps
1 parent ffd9fa8 commit b553b57

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

src/components/MapContainer.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
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) {
1919
mapProvider = new OpenStreetMapProvider(handleStopMarkerSelect);
2020
} else {

src/components/map/PopupContent.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<div class="transform rounded-lg bg-white p-4 shadow-lg">
77
<h3 class="text-xl font-bold text-gray-900">{stopName}</h3>
8-
<p class="mt-2 flex items-center text-gray-700">
8+
<p class="my-4 flex items-center text-gray-700">
99
<span class="mr-2 rounded-md bg-purple-600 px-2 py-1 text-white">Arrival time:</span>
1010
<span
1111
>{new Date(arrivalTime * 1000).toLocaleTimeString([], {

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));
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) {

0 commit comments

Comments
 (0)