【发布时间】:2018-07-19 20:09:28
【问题描述】:
我正在使用两个函数,第一个是使用地理定位 API,我想返回 lat 和 lng
在第二个函数中,我想用这个坐标获取一些数据 但我无法在第一个功能上正确导出它。
我知道 geolocationData() 不是函数。
这是我的代码
const geolocationData = () => {
return navigator.geolocation.getCurrentPosition((position) => {
return position
}, () => {
alert('Unable to fetch your location')
}, { enableHighAccuracy: true })
}
const gpsLocation = async () => {
const lat = geolocationData().position.coords.latitude
const lng = geolocationData().position.coords.longitude
const address = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&result_type=administrative_area_level_4&key=0000000000000000`)
const weatherData = await fetch(`https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/0000000000000/${lat},${lng}?units=si&extend=hourly&exclude=flags&lang=el`)
return {
address: address.json(),
weatherData: weatherData.json()
}
}
【问题讨论】: