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

Conversation

tarunsinghofficial
Copy link
Collaborator

Fixed #4

Tasks done:

  • Added a 'Find Current Location' button to the map.
  • Clicking the button first gets the user permission, and if allowed we center the map with their current location.

Screenshot:
bandicam2024-07-0520-55-03-272-ezgif com-video-to-gif-converter

Copy link
Member

@aaronbrethorst aaronbrethorst left a comment

Choose a reason for hiding this comment

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

The feature works great! I noted some code changes I'd like to see before merging. Please let me know via slack if you have any questions!

@@ -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.

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.

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.

@@ -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!

Copy link
Member

@aaronbrethorst aaronbrethorst left a comment

Choose a reason for hiding this comment

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

awesome, great work! 👏

@aaronbrethorst aaronbrethorst merged commit 36fe3d9 into main Jul 6, 2024
2 checks passed
@aaronbrethorst aaronbrethorst deleted the stop-marker branch July 6, 2024 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a 'Current Location' button to the map
2 participants