Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

APIs Using the Geolocation API in JavaScript

Can you get the user's current city name by using the Geolocation API?

I'm interested in getting the city name of the user, with the help of the Geolocation API. I have the longitude and latitude, but can I translate that into a city location? How can I go about getting this?

Thanks!

1 Answer

Steven Parker
Steven Parker
231,271 Points

You can't get city name directly from the geolocation API.

But you can get your location as latitude and longitude. And you can probably find a map service (possibly Google's) with an API that you can pass those coordinates to and get other information back including city name. You might use AJAX to do this.

And here's a documentation page about using Google's map API do to "reverse geocoding".

Awesome thank you! I've just started learning about AJAX, and i'll definitely check out Google's API and see what I can do.