Skip to content

Instantly share code, notes, and snippets.

@SerafimPoch
Last active February 12, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SerafimPoch/3cc7e54906b79d3ea4c96f489828d09f to your computer and use it in GitHub Desktop.
Save SerafimPoch/3cc7e54906b79d3ea4c96f489828d09f to your computer and use it in GitHub Desktop.
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");
}
});
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