Last active
February 12, 2018 11:57
-
-
Save SerafimPoch/3cc7e54906b79d3ea4c96f489828d09f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const key = "b8a59b301233a16e12819fc3b17ebc57"; | |
const showPosition = position => { | |
updateByGeo(position.coords.latitude, position.coords.longitude); | |
}; | |
export const updateByGeo = (lat, lon) => { | |
let url = | |
"https://api.openweathermap.org/data/2.5/forecast?" + | |
"lat=" + | |
lat + | |
"&lon=" + | |
lon + | |
"&APPID=" + | |
key; | |
return url; | |
}; | |
window.addEventListener("load", () => { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(showPosition); | |
} else { | |
alert("No internet connection"); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { updateByGeo } from "./Geo"; | |
class Weather extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
city: "London", | |
url: | |
"https://api.openweathermap.org/data/2.5/forecast?lat=56&lon=14&APPID=b8a59b301233a16e12819fc3b17ebc57" | |
}; | |
} | |
sendRequest = async url => { | |
const link = await fetch(url); | |
const data = await link.json(); | |
let city = data.city.name; | |
this.setState({ city }); | |
}; | |
async componentDidMount() { | |
this.sendRequest(updateByGeo); | |
} | |
render() { | |
return ( | |
<div> | |
/* content */ | |
</div> | |
); | |
} | |
} | |
export default Weather; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment