Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/polyline-arrow-symbol #36

Merged
merged 7 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/map/RouteMap.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
/* global google */
import { onMount, onDestroy } from 'svelte';
import { createPolyline, loadGoogleMapsLibrary } from '$lib/googleMaps';
import { createPolyline, loadGoogleMapsLibrary, addArrowToPolyline } from '$lib/googleMaps';

export let map;
export let tripId;
Expand Down Expand Up @@ -39,6 +39,7 @@

if (shape) {
polyline = await createPolyline(shape);
addArrowToPolyline(polyline);
polyline.setMap(map);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const COLORS = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Great you created the colors file separately. For better visibility, we can use the polyline arrow color as #8250DF.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! I will try it

POLYLINE: '#00FF00',
POLYLINE_ARROW: '#8250DF'
};
31 changes: 30 additions & 1 deletion src/lib/googleMaps.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { COLORS } from '$lib/colors';

/**
* Loads the Google Maps JavaScript library.
* @param {string} apiKey - The API key for accessing the Google Maps API.
Expand Down Expand Up @@ -154,10 +156,37 @@ export async function createPolyline(shape) {
const polyline = new window.google.maps.Polyline({
path,
geodesic: true,
strokeColor: '#00FF00',
strokeColor: COLORS.POLYLINE,
strokeOpacity: 1.0,
strokeWeight: 4
});

return polyline;
}

/**
* Adds an arrow to the polyline.
* @param {google.maps.Polyline} polyline - The polyline to which the arrow will be added.
*/
export function addArrowToPolyline(polyline) {
if (!(polyline instanceof google.maps.Polyline)) {
console.error('Invalid polyline');
return;
}
const arrowSymbol = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a check at the beginning of the function to validate the polyline parameter will be great, right @Ahmedhossamdev @aaronbrethorst? Something like this:

if (!(polyline instanceof google.maps.Polyline)) {
        console.error('Invalid polyline');
        return;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that’s a great idea for checking the polyline parameter. Moving to TypeScript could also improve type safety and help as the project grows.

path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
scale: 2,
strokeColor: COLORS.POLYLINE_ARROW,
strokeWeight: 3
};

polyline.setOptions({
icons: [
{
icon: arrowSymbol,
offset: '100%',
repeat: '50px'
}
]
});
}
1 change: 0 additions & 1 deletion src/routes/api/oba/shape/[shapeId]/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export async function GET({ params }) {

try {
const response = await fetch(apiURL);
console.log('response:', response);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
Expand Down
Loading