Skip to content

Instantly share code, notes, and snippets.

@darkrishabh
Last active September 29, 2020 06:26
Show Gist options
  • Save darkrishabh/3132ad32c0160b9dba93131b4ab388a2 to your computer and use it in GitHub Desktop.
Save darkrishabh/3132ad32c0160b9dba93131b4ab388a2 to your computer and use it in GitHub Desktop.
getCurrentLocation = async () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
resolve({
latitude: parseFloat(position.coords.latitude),
longitude: parseFloat(position.coords.longitude),
latitudeDelta: 5,
longitudeDelta: 5,
})
},
reject,
{
enableHighAccuracy: true,
timeout: 10000,
}
);
})
};
getTrailData = async ({ latitude: lat, longitude: lon }) => {
//GET request
var url = new URL("https://www.hikingproject.com/data/get-trails?");
var params = {
lat,
lon,
maxDistance: "10",
maxResults: "5",
key: "200910989-d4cede628914dc93c09781fa51730b39",
};
Object.keys(params).forEach((key) =>
url.searchParams.append(key, params[key])
);
alert(url);
return fetch(url)
.then((response) => response.json())
//If response is in json then in success
.then((responseJson) => {
// alert(responseJson.trails.length);
this.setState({
dataSource: responseJson.trails,
loaded: true,
});
// alert(this.state.dataSource.length);
})
// trails(responseJson.trails);
//If response is not in json then in error
.catch((error) => {
//Error
this.setState({
loaded: true,
error: true,
});
console.error(error);
});
};
try {
const region = await getCurrentLocation()
this.mapView.animateToRegion(region, 1000)
await getTrailData()
} catch (error) {
console.error(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment