【问题标题】:React Native - Getting the `location` state in my React Redux StoreReact Native - 在我的 React Redux 商店中获取“位置”状态
【发布时间】:2019-06-11 03:24:38
【问题描述】:

我试图在我的 React Redux Store 中获取我的 location(经度和纬度)状态以创建多个 MapView Marker,但它返回此错误:

这是我的代码:

EventMap.js - 这是我想使用 Redux State 并创建多个 MapView Marker 的地方。我尝试在 MapView.Marker 的坐标中应用 Redux 状态。

this.state = {
  focusedLocation: {
    latitude: 0,
    longitude: 0,
    // latitudeDelta: 0.04864195044303443,
    // longitudeDelta: 0.040142817690068,
    latitudeDelta: 0.01,
    longitudeDelta: Dimensions.get('window').width / Dimensions.get('window').height * 0.01
  },
  },
  markers: [
    {
      coordinate: {
        latitude: 37.42484589323653,
        longitude: -122.08271104842426
      },
    },
    {
      coordinate: {
        latitude: 37.42019338901534,
        longitude: -122.08207536488771
      },

    },
    {
      coordinate: {
        latitude: 37.4219108525511,
        longitude: -122.08126466721296
      },
    },
    {
      coordinate: {
        latitude: 37.42190153308783,
        longitude: -122.08728086203337
      },
    },
    {
      coordinate: {
        latitude: 37.419681603891306,
        longitude: -122.08521489053966
      },
    }
  ],
}

<MapView
      style={styles.container}
      initialRegion={this.state.focusedLocation}
      onPress={this.pickLocationHandler}
      showsUserLocation={true}
      ref={ref => this.map = ref} //For animating map movement
    >
      {userMarker}
      {this.props.events.map((marker, index) => {
        if(marker.location) {
          const scaleStyle = {
            transform: [
              {
                scale: interpolations[index].scale,
              },
            ],
          };
          const opacityStyle = {
            opacity: interpolations[index].opacity,
          };
          return (
            <MapView.Marker key={index} coordinate={marker.location}>
              <Animated.View style={[styles.markerWrap, opacityStyle]}>
                <Animated.View style={[styles.ring, scaleStyle]} />
                  <View style={styles.marker} />
              </Animated.View>
            </MapView.Marker>
          );
        } else {
          return null;
        }
      })}
    </MapView>

---------------
const mapStateToProps = state => {
return {
  events: state.events.events
};
};

export default connect(mapStateToProps)(EventMap);

EventCreator.js:

placeAddedHandler = () => {
this.props.onAddEvent(
  this.state.controls.eventName.value,
  this.state.controls.eventDescription.value,
  this.state.controls.location.value
);
};

render() {
return(
  <LinearGradient style={styles.linearGradient} colors={['#718792', '#1c313a', '#000a12']}>
    <View style={{flex:1}}>
      <SetLocationMap 
        mapContainer={styles.mapContainer}
        onLocationPick={this.locationPickedHandler}
        showUserLocation={true}
      />
    </View>
    <View style={styles.buttonContainer}>
      <TouchableOpacity onPress={this.placeAddedHandler}>
        <View style={styles.button}>
          <Text style={{color: 'black', fontSize: 20, fontWeight: 'bold'}}>Add</Text>
        </View>
      </TouchableOpacity>
    </View>
  </LinearGradient>
);
} 
}

SetLocationMap.js:

  pickLocationHandler = (event) => {
const coords = event.nativeEvent.coordinate;
//For animation of map
this.map.animateToRegion({
  ...this.state.focusedLocation,
  latitude: coords.latitude,
  longitude: coords.longitude
});
this.setState(prevState => {
  return {
    focusedLocation: {
      ...prevState.focusedLocation,
      latitude: coords.latitude,
      longitude: coords.longitude
    },
    locationChosen: true
  };
});
this.props.onLocationPick({
  latitude: coords.latitude,
  longitude: coords.longitude
});
};

  render() {
let marker = null;
if(this.state.locationChosen) {
  marker = <MapView.Marker coordinate={this.state.focusedLocation} flat={true}/>
}
return(
  <View style={this.props.mapContainer}>
    {/* <TouchableOpacity onPress={this.getLocationHandler} style={styles.iconContainer}>
      <Icon name="md-locate" size={30} color="blue"/>
    </TouchableOpacity> */}
    <MapView
      {...this.props}
      style={styles.map}
      initialRegion={this.state.focusedLocation}
      onPress={this.pickLocationHandler}
      ref={ref => this.map = ref} //For animating map movement
    >
      {marker}
    </MapView>
  </View>
);
}
}

这是我的 React Redux 代码:

../store/reducers/events.js - this is where I put my React Redux State

const reducer = (state = initialState, action) => {
switch (action.type) {
  case ADD_EVENT:
    return {
       ...state,
      events: state.events.concat({
        key:  `${Math.random()}`,
        name: action.eventName,
        description: action.eventDescription,
        location: action.location,
        image: {
          uri: "https://c1.staticflickr.com/5/4096/4744241983_34023bf303_b.jpg"
        }
      })
    };

如果缺少某些东西,请向我索要密码。谢谢!

【问题讨论】:

    标签: javascript reactjs react-native react-redux react-native-maps


    【解决方案1】:

    可能是您的代码中的问题是这样的:

    <MapView.Marker key={index} coordinate={this.props.events}> 
    
    

    this.props.events 与位置类型不同(根据您的州)。

    解决方案的一种尝试是尝试提供如下位置类型:

    <MapView.Marker key={index} coordinate={{latitude: latitude, longitude: longitude}}> 
    

    其中 latitudelongitude 是浮点数。

    例子:

    <MapView.Marker key={index} coordinate={{latitude: 4.3414303 , longitude: 15.3277476}}> 
    

    更新: 如果您的意图是显示您所在州的事件提供的位置,您可以这样做:我假设您所在州的位置是位置类型({纬度:纬度,经度:经度}),您可以直接循环 this.props.events :

    <MapView
          style={styles.container}
          initialRegion={this.state.focusedLocation}
          onPress={this.pickLocationHandler}
          showsUserLocation={true}
          ref={ref => this.map = ref} //For animating map movement
        >
          {userMarker}
          {this.props.events.map((marker, index) => {
            if(marker.location.latitude && marker.location.longitude) {
                const scaleStyle = {
                  transform: [
                    {
                      scale: interpolations[index].scale,
                    },
                  ],
                };
                const opacityStyle = {
                  opacity: interpolations[index].opacity,
                };
                return (
                  <MapView.Marker key={index} coordinate={marker.location}>
                    <Animated.View style={[styles.markerWrap, opacityStyle]}>
                      <Animated.View style={[styles.ring, scaleStyle]} />
                        <View style={styles.marker} />
                    </Animated.View>
                  </MapView.Marker>
                );
            } else {
                return null;
            }
          })}
        </MapView>
    

    【讨论】:

    • 如何使用您的示例/答案/建议访问我的 Redux 商店中的经度和纬度?
    • 我有一个很好奇的问题,如果我的locationevents(经度和纬度)的Redux Store 中没有任何值,是否会出现错误?
    • 是的,您应该提供一个坐标。我有一些问题:1- this.props.events 的类型是什么:数组、字符串或...? 2-当您使用地图指令循环变量 this.state.markers 时,我假设您想在地图上显示几个标记,请您尝试告诉您如何设置此变量?
    • 1.它是一个数组,包含 2 个字符串、1 个图像和位置值(经度和纬度)。 2. 是的,我想在地图上显示几个标记。我使用map 设置此变量以测试它是否可以在地图上显示多个标记。此外,我将此变量设置为将其用于动画,因为我有 Animated.ScrollView (5 Card) 所以每次我刷每张卡片时,它都会将其导航到它们自己的纬度和经度。我将在我的问题上添加我的this.state.markers 的代码。谢谢!
    • 我更新了我的答案,以防您想显示您所在州的每个事件的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2021-06-03
    • 2018-11-30
    • 1970-01-01
    • 2022-01-17
    • 2018-12-16
    相关资源
    最近更新 更多