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

Added the Geolocation (current location of user) Feature #16

Merged
merged 4 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

.custom-map-control-button {
background-color: #fff;
border: 0;
border-radius: 2px;
box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
margin: 10px;
padding: 0 0.3em;
font-size: 25px;
color: #666;
overflow: hidden;
height: 40px;
cursor: pointer;
position: absolute;
bottom: 4em;
right: 0;
z-index: 5;
}
.custom-map-control-button:hover {
background: rgb(235, 235, 235);
color: black;
}
42 changes: 42 additions & 0 deletions src/components/map/GoogleMap.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
/* global google */
import { browser } from '$app/environment';
import { createEventDispatcher, onMount, onDestroy } from 'svelte';
import {
Expand Down Expand Up @@ -44,6 +45,43 @@
if (browser) {
window.addEventListener('themeChange', handleThemeChange);
}

const locationButton = document.createElement('button');
Copy link
Member

Choose a reason for hiding this comment

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

I'd love to see this extracted out into a reusable component. We'll want to support different map backends at some point (Mapbox, OSM, Bing, Apple, etc.), and I want to make sure that we can reuse as much functionality across them as possible.

Once it's a reusable component, it should be straightforward (I hope) to use the Font Awesome package to display the crosshairs icon instead of loading the entire package from a CDN.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay. I will make sure to create a reusable component for using in the future.


locationButton.innerHTML = '<i class="fa-solid fa-location-crosshairs"></i>';
locationButton.classList.add('custom-map-control-button');
document.getElementById('map').appendChild(locationButton);

locationButton.addEventListener('click', () => {
if (navigator.geolocation) {
Copy link
Member

Choose a reason for hiding this comment

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

Stylistically, I prefer early returns over deep nesting. it makes it easier to read your code and follow the control flow. What I mean by this here is to flip your conditions around:

if (!navigator.geolocation) {
  alert('Geolocation is not supported by this browser.');
  return;
}

// ... continue on with the rest of your logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, that's a great approach. I have added the early return statement.

navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
const userLocation = new google.maps.LatLng(latitude, longitude);
map.setCenter(userLocation);

new google.maps.Marker({
Copy link
Member

Choose a reason for hiding this comment

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

since we want the button to be a reusable component, can you keep this marker logic here and invoke it with a callback from the LocationButton component?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done this one.

map: map,
position: userLocation,
title: 'Your Location',
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
fillColor: '#007BFF',
fillOpacity: 1,
strokeWeight: 2,
strokeColor: '#FFFFFF'
}
});
},
() => {
alert('Unable to retrieve your location.');
}
);
} else {
alert('Geolocation is not supported by this browser.');
}
});
}

async function loadStopsAndAddMarkers(lat, lng) {
Expand Down Expand Up @@ -104,6 +142,10 @@
});
</script>

<link
Copy link
Member

Choose a reason for hiding this comment

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

make sure to remove this after making the other changes!

rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css"
/>
<div id="map"></div>

<style>
Expand Down