Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React本机地理位置变量不可访问_Javascript_React Native_Expo - Fatal编程技术网

Javascript React本机地理位置变量不可访问

Javascript React本机地理位置变量不可访问,javascript,react-native,expo,Javascript,React Native,Expo,这是我的问题,我现在正在开发一个react本机应用程序,除了函数getLocation(),所有的代码都可以工作。它的工作原理是返回纬度和经度,但由于某些原因,它花费的时间太长,当我运行一个依赖于这些值的函数时,调用该函数时它不会及时运行。是否有任何方法可以延迟函数,使其在getLocation()函数返回时具有纬度和经度 我之前做过一些研究,这是另一个类似的堆栈溢出问题,但是他们使用了不同的方法来获取位置。 任何帮助都将不胜感激 import React, { Component } fro

这是我的问题,我现在正在开发一个react本机应用程序,除了函数getLocation(),所有的代码都可以工作。它的工作原理是返回纬度和经度,但由于某些原因,它花费的时间太长,当我运行一个依赖于这些值的函数时,调用该函数时它不会及时运行。是否有任何方法可以延迟函数,使其在getLocation()函数返回时具有纬度和经度

我之前做过一些研究,这是另一个类似的堆栈溢出问题,但是他们使用了不同的方法来获取位置。

任何帮助都将不胜感激

import React, { Component } from 'react';
import { ActivityIndicator, ListView, Text, View, Picker } from 'react-native';

export default class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoading: true,
            dataSource: null,
            key: "KEY NOT SHOWN BUT WORKS",
            latitude: null,
            longitude: null,
            error: 2
        };
    }


    /**
     * This function gets the current geo location of the device and stores it
     * as Lat and Long coordinates.
     * @private
     */
    getLocation(){
        navigator.geolocation.getCurrentPosition(
            (position) => {
                this.setState({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                });
            },
            (error) =>
                this.setState({ error: error.message, latitude: 41.77, longitude: -88.07,}),
            { enableHighAccuracy: false, timeout: 5000, maximumAge: 10000 },
        );
    }

    getAPI(){
        fetch('https://developer.cumtd.com/api/v2.2/JSON/getstopsbylatlon?key='+this.state.key+'&lat='+this.state.latitude.toString()+'&lon='+this.state.longitude.toString())
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    dataSource: responseJson.stops,
                    isLoading: false,
                }, function() {
                    // do something with new state
                });
            })
            .catch((error) => {
                //error
            });
    }

    componentDidMount() {
        this.getLocation();
        this.getAPI();
    }



    render() {
        if (this.state.isLoading) {
            return (
                <View style={{flex: 1, paddingTop: 20}}>
                    <ActivityIndicator />
                </View>
            );
        }
        return (
            <View style={{flex: 1, paddingTop: 20}}>
                <Text>Latitude: {this.state.latitude}</Text>
                <Text>Longitude: {this.state.longitude}</Text>
                <Picker
                    selectedValue={this.state.dataSource}>
                    {this.state.dataSource.map((item, key)=>(
                        <Picker.Item label={item.stop_name} value={item.stop_name} key={key}/>))}
                </Picker>
            </View>
        );
    }
}
import React,{Component}来自'React';
从“react native”导入{ActivityIndicator,ListView,Text,View,Picker};
导出默认类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
孤岛加载:是的,
数据源:null,
密钥:“密钥未显示但有效”,
纬度:空,
经度:空,
错误:2
};
}
/**
*此函数用于获取设备的当前地理位置并将其存储
*作为横向和纵向坐标。
*@私人
*/
getLocation(){
navigator.geolocation.getCurrentPosition(
(职位)=>{
这是我的国家({
纬度:位置。坐标。纬度,
经度:position.coords.longitude,
});
},
(错误)=>
this.setState({error:error.message,纬度:41.77,经度:-88.07,}),
{enableHighAccurance:false,超时:5000,最大值:10000},
);
}
getAPI(){
取('https://developer.cumtd.com/api/v2.2/JSON/getstopsbylatlon?key=“+this.state.key+”&lat='+this.state.latitude.toString()+”&lon='+this.state.longitude.toString())
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
数据源:responseJson.stops,
孤岛加载:false,
},函数(){
//对新国家做些什么
});
})
.catch((错误)=>{
//错误
});
}
componentDidMount(){
这个.getLocation();
这是getAPI();
}
render(){
if(此.state.isLoading){
返回(
);
}
返回(
纬度:{this.state.Latitude}
经度:{this.state.Longitude}
{this.state.dataSource.map((项,键)=>(
))}
);
}
}

检索位置后,只需将API调用移动到
getLocation()
。 当您集成权限处理时,您将希望转向更基于承诺的结构

检索位置后,只需将API调用移动到
getLocation()

当您集成权限处理时,您将希望转向更基于承诺的结构。地理位置API是异步的,所以当信息还不可用时,您必须找到一种方法来处理这些事情。真的没有办法让它更快。此外,用户很可能拒绝位置权限请求,在这种情况下,您将永远无法获得lat/long。因此,您不妨接受它,并找出一些东西来显示时,lat/long是不可用的。感谢您的快速回复!我一直在想,做一个布尔值,当它改变值时,把它设为真,然后我需要一个循环不断地检查它。有没有更有效的方法呢?当您给
getCurrentPosition
的成功回调执行时,它会返回一个值。因此,您需要将依赖代码放在该回调中,或者,您可以包装
getCurrentPosition
调用,使其返回一个承诺,然后使用promise
。然后
方法。但无论哪种方式,你都必须接受它的异步性。这是有道理的!谢谢地理位置API是异步的,所以当信息还不可用时,您必须找到一种方法来处理这些事情。真的没有办法让它更快。此外,用户很可能拒绝位置权限请求,在这种情况下,您将永远无法获得lat/long。因此,您不妨接受它,并找出一些东西来显示时,lat/long是不可用的。感谢您的快速回复!我一直在想,做一个布尔值,当它改变值时,把它设为真,然后我需要一个循环不断地检查它。有没有更有效的方法呢?当您给
getCurrentPosition
的成功回调执行时,它会返回一个值。因此,您需要将依赖代码放在该回调中,或者,您可以包装
getCurrentPosition
调用,使其返回一个承诺,然后使用promise
。然后
方法。但无论哪种方式,你都必须接受它的异步性。这是有道理的!谢谢这个解决方案有效!真不敢相信这么简单。这个解决方案行得通!真不敢相信事情竟那么简单。