Hello,
I’ve been searching for a way to get a users IP address that i can grab and use with the https://ipapi.co and then use that to make a request to the open weather maps api. Is their a way to do this with just javascript, or am i missing something on sending the ipapi request?
Thanks!
Never mind. Have been overthinking it again.
Thanks!
Hi there,
If you just make a “GET” request to a location api, (I use http://ip-api.com/json ) using either JavaScript’s native XMLHttpRequest
object or jQuery’s $.ajax()
, then it will respond with a JSON which includes the user’s location information.
Armed with that, you can make a new request to your weather api with the city name in your query string. Something like:
“https://someweatherAPI/data/q=” + cityFromFirstRequest + “&API_KEY=som ekey”
This should return the weather information for the particular location
Weather underground API has an autoip feature you might be interested in.
I used https://ipapi.co. You don’t need a users IP to get the JSON response. You can make a request like this: https://ipapi.co/json. That will return the ip and gps coordinates. You’ll also need to consider using JSONP for the weather. You’ll know if you’re getting any Access-Control-Allow-Origin errors.