javascript - 向标记添加不同图像的功能 – React-native MapView

标签 javascript json react-native sorting

本文的使用https://medium.com/@clarkjohnson_85334/adding-fetched-markers-to-my-react-native-maps-app-5f068c7be14d ,我已经成功地将观察结果集成到我的 MapView 中。

 mapMarkers = () => {
    return this.state.items.map((item) => <Marker
        key={item.id}
        coordinate={{ latitude: item.geoLocation.latitude, longitude: item.geoLocation.longitude }}
        title={item.acceptedVernacularName}
        description={item.scientificName}
    >
    </Marker >)
}

但是,我还没有设法创建一个根据鸟的类型向标记添加不同图像的功能。是否有可能创建一个可以与需要字符串的 Marker image={} 属性一起使用的函数?我的想法是,图像应该使用 require('../assets/xx') 设置为本地镜像,但基于 id 或鸟的名称 (acceptedVernacularName)。

JSON file

最佳答案

创建一个这样的函数来根据您的类型获取自定义图标

getIconFromType(type) {
        var icon = require("../image_path/");
        switch (type) {
            case "A": icon = require("../image_path/image_a.png");
                break
            case "B": icon = require("../image_path/image_b.png");
                break
            default: icon = require("../image_path/image_b.png");
                break
        }
        return icon
 }

然后使用这种方式根据您的 ID 在标记中应用自定义图标

<Marker  ...other props>
 <Image source={this.getIconFromType(item.type)} resizeMode={'contain'} style={{ height: 30, width: 30 }} />
</Marker>

关于javascript - 向标记添加不同图像的功能 – React-native MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65221800/

相关文章:

javascript - 如何将 React 开发工具与 React Native 一起使用?

javascript - 在 javascript 中扩展类

javascript - 如何使用jquery获取点击按钮时输入字段的值?

java - Jackson Json,编码 ByteBuffer

javascript - 对 json 进行排序和模式匹配

android - 为什么 Facebook 应用程序链接在 React Native 中不起作用

javascript - 进行选择下拉列表而不显示值

javascript - 在 HTML 页面中使用 JavaScript 读取/查询 Google 电子表格

javascript - 无法检查空 JSON 键 : value pair

reactjs - 无法执行JS调用: __fbBatchedBridge is undefined