更新由以下人员管理的视图的属性“坐标"时出错:AIRMapMarker(React native) [英] Error while updating property 'coordinate' of a view managed by: AIRMapMarker (React native)

查看:45
本文介绍了更新由以下人员管理的视图的属性“坐标"时出错:AIRMapMarker(React native)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索有关此错误的正确文档,但我很不走运,因为我无法确定此错误的原因.

I have been searching the web for proper documentation in regards to this error, am in in no luck as i am unable to determine the cause of this error.

这是我的全部代码:

第一部分:设置状态

export default class Whereto extends Component<{}> {

constructor(props) {
    super(props);

    this.state = {
        latitude: null,
        longitude: null,
        location: null,
        error: null,
        markers:[],

    };
}

第二部分组件安装

componentDidMount() {
        navigator.geolocation.getCurrentPosition(
            (position) => {
                this.setState({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                    error: null,
                });

                //geocode api
                var myApiKey = '';

                fetch('https://maps.googleapis.com/maps/api/geocode/json?address=' + position.coords.latitude + ',' + position.coords.longitude + '&key=' + myApiKey)
                    .then((response) => response.json())
                    .then((responseJson) => {
                        //console.log('ADDRESS GEOCODE is BACK!! => ' + JSON.stringify(responseJson));
                        var locationName = responseJson.results[0].address_components.filter(x => x.types.filter(t => t === 'administrative_area_level_2').length > 0)[0].short_name;
                        //console.log(locationName);
                        this.setState({
                            location: locationName,
                        })
                    })

                //nearby api
                var apiPlaceskey = '';
                //https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key=YOUR_API_KEY

                fetch('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + position.coords.latitude + ',' + position.coords.longitude + '&radius=2000&type=bus_station&key=' + apiPlaceskey)
                    .then((respplaces) => respplaces.json())
                    .then((responseJson2) => {

                        const markers = responseJson2.results.map((result) => ({
                            latlng: {
                                latitude: result.geometry.location.lat,
                                longitude: result.geometry.location.lng,
                            }
                        }));

                        this.setState({ markers });
                    });


            },
            (error) => this.setState({error: error.message}),
            {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},

        );


    }

第三部分:在渲染和查看部分点击可触摸按钮时保留的功能

Third section: A function reserved when a a touchable button is tapped on my render and View section

fetchDirections = () => {
        //directions api
        var apiDirectionskey = '';
        //const {location} = this.state;
        const {latitude} = this.state;
        const {longitude} = this.state;

        fetch('https://maps.googleapis.com/maps/api/directions/json?origin=' + latitude + ',' + longitude + '&destination=' + goingto + '&mode=transit&transit_mode=bus&key=' + apiDirectionskey)
            .then((resdirections) => resdirections.json())
            .then((responseJson3) => {

                console.log(responseJson3);

            });


        }

    render(){

        return(
            <View style={styles.container}>
                <Mainlogo/>
                <TextInput style={styles.boxInput} underlineColorAndroid='rgba(0,0,0,0)' placeholder="Going To?"
                           underlineColorAndroid='transparent'
                           onChangeText={(dest) => this.setState({goingto : dest})}
                />
                <TouchableOpacity style={styles.button} onPress={this.fetchDirections.bind(this)}>
                    <Text style={styles.textButton}> Go {this.props.type}</Text>
                </TouchableOpacity>

                <MapView style={styles.map}
                         region={{
                             latitude: this.state.latitude,
                             longitude: this.state.longitude,
                             latitudeDelta: 0.02,
                             longitudeDelta: 0.02
                         }}
                >

                </MapView>

                <MapView.Marker
                    coordinate={{
                        latitude: this.state.latitude,
                        longitude: this.state.longitude,
                        latitudeDelta: 0.02,
                        longitudeDelta: 0.02
                    }}
                    image={require('../img/my-pin-512.png')}
                    title={'you are here'}
                />

                {this.state.markers.map(marker => (
                    <MapView.Marker
                        coordinate={marker.latlng}
                        image={require('../img/busstop.png')}

                    />

                ))}





            </View>
        )

    }
}

为了进入这个阶段,从我的主个人资料页面点击了一个可触摸的不透明度.我意识到我正在使用 componendDidMount 和一个单独的 fetch 函数来调用另一个 API 调用.似乎没有足够的时间来生成导致空值的状态

To get to this stage a touchableopacity is tapped from my main profile page. I realised that i am using componendDidMount and a seperate fetch function to call another API call. It seems that it is not getting enough time to pupulate the states to cause the null values

推荐答案

将初始状态值设置为 0 而不是 null.

Set initial state values to 0 instead of null.

this.state = {
  latitude:0,
  longitude: 0,
  latitudeDelta: 0.09,
  longitudeDelta: 0.02,
};

这篇关于更新由以下人员管理的视图的属性“坐标"时出错:AIRMapMarker(React native)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆