【问题标题】:How to show markers on map fetching data from a JSON file on React Native如何在地图上显示标记从 React Native 上的 JSON 文件中获取数据
【发布时间】:2021-08-31 07:46:21
【问题描述】:

我正在从这里获取一些数据:https://jsonplaceholder.typicode.com/users 结构如下:

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
  },
...
]

这是我的代码,我可以在其中获取这些数据并将它们显示在平面列表中


export default class Source extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            dataSource: []
        };
    }
    componentDidMount() {
        fetch("https://jsonplaceholder.typicode.com/users")
            .then(response => response.json())
            .then((responseJson) => {
                this.setState({
                    loading: false,
                    dataSource: responseJson
                })
            })
            .catch(error => console.log(error)) //to catch the errors if any
    }

    renderItem = (data) =>
        <TouchableOpacity >
            <Text style={styles.lightText}>{data.item.name}</Text>
            <Text style={styles.lightText}>{data.item.address.geo.lng}</Text>
            <Text style={styles.lightText}>{data.item.address.geo.lat}</Text></TouchableOpacity>

    render() {
        return (
            <View>
                <FlatList
                    data={this.state.dataSource}
                    ItemSeparatorComponent={this.FlatListItemSeparator}
                    renderItem={item => this.renderItem(item)}
                    keyExtractor={item => item.id.toString()}
                />
            </View>
        )
    }
}

如何使用这些纬度和经度数据在 MapView 中显示它们?此代码将显示 2 个标记,我需要根据该 json 文件显示标记

  <MapView style={styles.map}>
        <MapView.Marker
          coordinate={{
            latitude: 45.441831,
            longitude: 9.190771
          }}
        />
        <MapView.Marker
          coordinate={{
            latitude: 46.2222,
            longitude: 11.1231
          }}
        />
</MapView>

【问题讨论】:

    标签: json reactjs native markers


    【解决方案1】:

    编辑 renderItem 函数:

    renderItem = (data) => {
            return(
                <TouchableOpacity style={{flex:1}}>
                        <Text style={styles.lightText}>{data.item.name}</Text>
                        <MapView
                        style={{ height:300 }}
                        initialRegion={{
                            latitude: Number(data.item.address.geo.lat),
                            longitude: Number(data.item.address.geo.lng),
                            latitudeDelta: 0.0922,
                            longitudeDelta: 0.0421,
                        }}
                        >
                        <MapView.Marker
                            coordinate={{
                            latitude: Number(data.item.address.geo.lat),
                            longitude: Number(data.item.address.geo.lng),
                            }}
                        />
                    </MapView>
                </TouchableOpacity>
            )
        }
    

    【讨论】:

    • 但是像这样我得到了一个平面列表,所以我看到了很多地图,我只需要看到一个有很多标记的地图
    猜你喜欢
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2015-06-09
    • 2019-08-22
    • 2017-08-08
    • 2015-11-25
    • 2021-02-25
    • 1970-01-01
    相关资源
    最近更新 更多