【问题标题】:React native maps with conditional markers使用条件标记反应原生地图
【发布时间】:2023-03-29 09:39:02
【问题描述】:

我正在使用 firebase 和 react 原生地图开发应用程序,我有

this.props.currentUser[
    name, 
    bloodType
]

this.state.users[
    name,
    bloodType
]

我想显示,例如,如果 currentUser 有血型:A,我想在地图中显示标记用户血型:A 和 AB。目前我只能显示 currentUser: A then users: A 我无法显示 AB。这是我到目前为止所做的。

<MapView

{this.state.users.filter(element => {
              let bloodCheck = this.props.currentUser.bloodType === element.bloodType  && element.geo_point.latitude 
            !== this.props.currentUser.geo_point.latitude && element.geo_point.longitude 
            !== this.props.currentUser.geo_point.longitude && element.locationOn === true ;
            let distance = this.calculateDistance(this.props.currentUser.geo_point.latitude, this.props.currentUser.geo_point.longitude,
            element.geo_point.latitude , element.geo_point.longitude); console.log(element.bloodType, bloodCheck)
            if(bloodCheck === true){
              return distance < 15000;
            }
            else{
              return false;
            }.map((user, idx) => <Marker
            key={idx}
            coordinate={{latitude: user.geo_point.latitude, longitude: user.geo_point.longitude}}
            >
</Marker>)}

</MapView>

【问题讨论】:

    标签: javascript firebase react-native react-native-maps


    【解决方案1】:

    您可以根据自己的情况创建函数来检查血型值

    check = (bloodType1, bloodType2) => {
            var AType = ["A", "AB"];
            var BType = ["B", "AB"];
            var ABType = ["A", "B", "AB"];
            var OType = ["A", "B", "AB", "O"];
            if (bloodType1 == 'A' && AType.includes(bloodType2, 0)) {
                return true;
            } else if (bloodType1 == 'B' && BType.includes(bloodType2, 0)) {
                return true;
            } else if (bloodType1 == 'AB' && ABType.includes(bloodType2, 0)) {
                return true;
            } else if (bloodType1 == 'O' && OType.includes(bloodType2, 0)) {
                return true;
            } else {
                return false
            }
        }
    

    简单地将你的价值传递给函数

    let bloodCheck = this.check(this.props.currentUser.bloodType, element.bloodType)  && element.geo_point.latitude 
                !== this.props.currentUser.geo_point.latitude && element.geo_point.longitude 
                !== this.props.currentUser.geo_point.longitude && element.locationOn === true ;
    

    【讨论】:

    • 感谢您的回答,但是 AType.includes(bloodType2, 0) 中的参数是什么意思?
    • 检查用户的血型是否属于给定的数组。
    猜你喜欢
    • 2020-10-28
    • 2019-02-28
    • 2018-12-09
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多