Javascript 向标记添加不同图像的函数–;反应本机地图视图

Javascript 向标记添加不同图像的函数–;反应本机地图视图,javascript,json,react-native,sorting,Javascript,Json,React Native,Sorting,通过使用本文,我成功地将观察结果集成到MapView中 mapMarkers = () => { return this.state.items.map((item) => <Marker key={item.id} coordinate={{ latitude: item.geoLocation.latitude, longitude: item.geoLocation.longitude }} title={item

通过使用本文,我成功地将观察结果集成到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 >)
}
mapMarkers=()=>{
返回此.state.items.map((item)=>
)
}
但是,我还没有成功地创建一个函数,根据鸟的类型为标记添加不同的图像。甚至可以创建一个可以使用需要字符串的Marker image={}prop的函数吗?我的想法是,应该使用require('../assets/xx')将图像设置为本地,但应基于鸟的id或名称(AcceptedLocalurname)


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

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>


如果解决了您的问题,请务必标记为“正确”。@goldcastlet非常感谢您!:D