Expo TaskManager startLocationUpdateAsync不会触发

问题描述 投票:0回答:1

我目前正在尝试开发一个应用,我需要在5到5秒内检查用户的位置。在iO上可以完美运行(但是每秒更新一次,因为我无法更改),但是在Android上,位置图标甚至没有出现在StatusBar中,因此该功能或任务管理器尚未触发。有什么办法吗?这是所有代码:

import React, { Component } from 'react';
import { View, Platform, Dimensions } from 'react-native'
import { Constants, Location, Permissions, TaskManager, } from 'expo';
import MapView, { PROVIDER_GOOGLE, Marker } from 'react-native-maps';
import { Icon } from 'native-base'
import { EventEmitter } from 'fbemitter'

const DRIVER_LOCATION = 'driver_location';
const DRIVER_LOCATION_EVENT = 'driver_location_event';
const eventEmitter = new EventEmitter();

let SCREEN_WIDTH = Dimensions.get('window').width;
let SCREEN_HEIGHT = Dimensions.get('window').height;

class HomeScreen extends Component {
    state = {
        mapRegion: null,
        hasLocationPermissions: false,
        locationResult: null,
        marker: {
            latitude: 0,
            longitude: 0
        },
        latitude: 0,
        longitude: 0,
        location: null,
        region: {},
        orientation: 1
    }

    static navigationOptions = {
        title: "Acasa",
        drawerIcon: <Icon name="md-map" style={{ fontSize: 25 }} />
    }
    componentWillMount() {
        Dimensions.addEventListener('change', () => {
            SCREEN_WIDTH = Dimensions.get('window').width;
            SCREEN_HEIGHT = Dimensions.get('window').height;
            if (SCREEN_HEIGHT > SCREEN_WIDTH)
                this.setState({ orientation: 1 })
            else
                this.setState({ orientation: 2 })
        })
    }

    componentDidMount() {
        this.eventSubscription = eventEmitter.addListener(DRIVER_LOCATION_EVENT, data => {
            this.setState({ location: { longitude: data.locations[0].coords.longitude, latitude: data.locations[0].coords.latitude } });
        });
        this.onLoad();
    }
    onLoad = async () => {
        let isRegistered = await TaskManager.isTaskRegisteredAsync(DRIVER_LOCATION)
        if (!isRegistered) await Location.startLocationUpdatesAsync(DRIVER_LOCATION, {
            accuracy: Location.Accuracy.Lowest,
            timeInterval: 2500,
        })
    }
    render() {
        console.log(this.state.location, 'location');
        return (
            <View style={{ flex: 1 }}>
                <MapView provider={PROVIDER_GOOGLE} style={{ flex: 1, position: 'absolute', zIndex: -1, width: SCREEN_WIDTH, height: SCREEN_HEIGHT }}
                    region={this.state.region}
                >
                    {this.state.location !== null ? <Marker coordinate={{ longitude: this.state.location.longitude, latitude: this.state.location.latitude }} /> : null}
                </MapView>
                <Icon name="menu" style={{ position: 'absolute', marginTop: Platform.select({ ios: 35, android: 20 }), marginLeft: 15, fontSize: 50, zIndex: 101 }} onPress={() => {
                    this.setState({ isMapVisible: false })
                    this.props.navigation.openDrawer()
                }} />
                <Icon name="md-locate" style={{ position: 'absolute', bottom: Platform.select({ ios: 35, android: 20 }), right: 15, fontSize: 50, zIndex: 101 }} onPress={() => {
                    Permissions.getAsync('location')
                        .then((res) => {
                            if (res.status === 'granted') {
                                Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.BestForNavigation })
                                    .then((res) => {
                                        this.setState({
                                            region: {
                                                latitude: res.coords.latitude,
                                                longitude: res.coords.longitude,
                                                latitudeDelta: 0.06,
                                                longitudeDelta: 0.03
                                            }
                                        })
                                    })
                            }
                            else {
                                Permissions.askAsync('location')
                                    .then((res) => {
                                        if (res.status === 'granted') {
                                            Location.getCurrentPositionAsync()
                                                .then((res) => {
                                                    this.setState({
                                                        region: {
                                                            latitude: res.coords.latitude,
                                                            longitude: res.coords.longitude,
                                                            latitudeDelta: 0.06,
                                                            longitudeDelta: 0.03
                                                        }
                                                    })
                                                })
                                        }
                                    })
                            }
                        })
                }} />
            </View>
        )
    }
}

export default HomeScreen;

TaskManager.defineTask(DRIVER_LOCATION, ({ data, error }) => {
    if (error) {
        console.log(error)
        return;
    }
    if (data) {
        eventEmitter.emit(DRIVER_LOCATION_EVENT, data);
    }
});
react-native react-native-android expo
1个回答
0
投票

您发现解决方法了吗?

© www.soinside.com 2019 - 2024. All rights reserved.