-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 2 commits
9e102a3
108f626
a99341a
af4900e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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 { | ||
|
@@ -44,6 +45,43 @@ | |
if (browser) { | ||
window.addEventListener('themeChange', handleThemeChange); | ||
} | ||
|
||
const locationButton = document.createElement('button'); | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -104,6 +142,10 @@ | |
}); | ||
</script> | ||
|
||
<link | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.