Geolocation API

Johnson Kow
2 min readAug 5, 2021

--

Shameless photo of myself

Okay, I’m not sure how people build projects while they have a full time job. I’m talking fullstack projects with full CRUD backend services and JWT authentication. How do you find the time after work!?

What I’ve been doing is building very small components just to keep fresh on my HTML, CSS, and Javascript skills. Recently I checked out AJAX to create api calls because I’ve been so accustomed to using Fetch. In the process I found something that I was not expecting to come across.

You ever use a website or an application that asks if you’d like to share your location? Snapchat, Instagram, and some dating apps do it and I’ve always wondered how they do that. I understand the ‘why’ but I never understood the ‘how’.

I just discovered the geolocation api inside of javascript. So I was creating a small component where you click a button that will return and display your coordinates. I won’t post my coordinate but let’s take a look at the code.

Okay I’m going to skip over jquery and just go straight into the click event. I had no idea that the navigator object had a geolocation attribute. With that attribute comes a function called getCurrentPostition() that accepts a callback function. Now this confused me a bit, understanding where the position parameter came from but it’s part of the function!

Position is an object that holds your latitude and longitudinal points. This would have saved me so much time in one of my project but instead, I used TWO google api services. So why does this matter? If you’ve ever used yelp to find food, you know that it recommends areas based on proximity to your location. Even Uber asks for you location for the pick up spot. See how it’s all coming together?

Maybe it’s just me but I found this to be mind blowing. It also requires the user to accept the call. So in case you’re a private person that doesn’t want to share that information, you don’t have to. This might just make me dive into a rabbit hole to understand how yelp finds all places nearby that match ‘ramen’. In the words of the young, ‘ this is dope’.

--

--

Johnson Kow

Software Engineer based out of NYC. Learning more about programming everyday 👍