Приложение React Native перезапускается при переходе на страницу с помощью response-native-maps-direction - PullRequest
1 голос
/ 27 мая 2020

Я использую expo и реагирую родным. У меня есть около 8 разных экранов, между которыми я перемещаюсь, но при переходе на страницу, которая должна отображать карту, приложение перезапускается и возвращается к экрану spla sh. Код отлично работает в эмуляторе и моем приложении expo. Но ошибка возникает в двоичной сборке (APK). Эта страница содержит данные из response-native-maps и react-native-maps-routes.

Я попытался заменить эту страницу простой страницей hello world, и тогда она отлично работает. Просто этот конкретный код карты работает не так, как ожидалось. Я использую:

"expo": "~ 37.0.3", "react": "~ 16.9.0", "react-native-maps": "0.26.1", "react- native-maps-direction ":" ^ 1.8.0 ",

Я не могу понять, почему это происходит. Я прикрепил код ниже. Спасибо.

import React, { Component } from 'react';
import { Dimensions, StyleSheet } from 'react-native';
import MapView from 'react-native-maps';
import MapViewDirections from 'react-native-maps-directions';

const { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE = 37.771707;
const LONGITUDE = -122.4053769;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;

const GOOGLE_MAPS_APIKEY = 'MY_KEY';

class MapScreen extends Component {

  constructor(props) {
    super(props);
    this.state = {
      coordinates: [
        {
          latitude: 12.822960,
          longitude: 80.041929,
        },
        {
          latitude: 13.009027, 
          longitude: 80.222075,
        },
      ],
    };

    this.mapView = null;
  }

  onMapPress = (e) => {
    this.setState({
      coordinates: [
        ...this.state.coordinates,
        e.nativeEvent.coordinate,
      ],
    });
  }

  render() {
    return (
      <MapView
        initialRegion={{
          latitude: LATITUDE,
          longitude: LONGITUDE,
          latitudeDelta: LATITUDE_DELTA,
          longitudeDelta: LONGITUDE_DELTA,
        }}
        style={StyleSheet.absoluteFill}
        ref={c => this.mapView = c}
        onPress={this.onMapPress}
      >
        {this.state.coordinates.map((coordinate, index) =>
          <MapView.Marker key={`coordinate_${index}`} coordinate={coordinate} />
        )}
        {(this.state.coordinates.length >= 2) && (
          <MapViewDirections
            origin={this.state.coordinates[0]}
            destination={this.state.coordinates[this.state.coordinates.length-1]}
            apikey={GOOGLE_MAPS_APIKEY}
            strokeWidth={3}
            strokeColor="hotpink"
            optimizeWaypoints={true}
            onStart={(params) => {
              console.log(`Started routing between "${params.origin}" and "${params.destination}"`);
            }}
            onReady={result => {
              console.log(`Distance: ${result.distance} km`)
              console.log(`Duration: ${result.duration} min.`)

              this.mapView.fitToCoordinates(result.coordinates, {
                edgePadding: {
                  right: (width / 20),
                  bottom: (height / 20),
                  left: (width / 20),
                  top: (height / 20),
                }
              });
            }}
            onError={(errorMessage) => {
              // console.log('GOT AN ERROR');
            }}
          />
        )}
      </MapView>
    );
  }
}

export default MapScreen;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...