【发布时间】:2018-11-05 17:56:40
【问题描述】:
我有一个 Geolocation.js,它获取地理位置并将其记录到控制台。这被导入到 index.js 文件中,需要使用记录的 lat 和 lng 值,以替换 ll 中的当前硬编码值:
import * as fsAPI from "../data/API_credentials";
import {userGeo} from "../component/Geolocation"
class Helper {
static baseURL() {
return "https://api.foursquare.com/v2";
}
// Client ID, client secret, and version stored in credentials file
static auth(){
const keys = {
client_id: `${fsAPI.client_id}`,
client_secret: `${fsAPI.client_secret}`,
v: `${fsAPI.client_version}`,
// Trying to get data from {userGeo}
// ll: `${userGeo.pos.lat}` + "," + `${userGeo.pos.lng}` //cannot read 'pos' of undefined.
// Line below works
ll: "36.04,-86.74"
}
return Object.keys(keys).map(key => `${key}=${keys[key]}`)
.join("&");
}
我作为userGeo 导入上述代码的代码如下,这是我的错误。我如何重构它以允许我在上面的ll 中获取lat lng? userGeo.pos.lat 例如。
你可以在上面的 // ll: `${userGeo.pos.lat}` + "," + `${userGeo.pos.lng}` 中看到我失败的尝试,它抛出了 cannot read 'pos' of undefined.
// Geolocation of user to be used for search query Foursqaure call url
export const userGeo = navigator.geolocation.getCurrentPosition((position) => {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
console.log(pos) // works
console.log("Lat: " + pos.lat) // works
});
非常感谢! 本
【问题讨论】:
标签: javascript reactjs syntax geolocation