I am trying to map my data to create map markers:
import ShellIcon from '../../../assets/images/xxx.svg';
import BPIcon from '../../../assets/images/xxx.svg';
import TexacoIcon from '../../../assets/images/xx.svg';
return (
<View style={styles.container}>
...
<MapView
..>
{staticData.map((marker, i) => {
const MarkerIcon = marker.type;
return (
<Marker key={index} coordinate={item.coordinates} ref={markerRef}>
<MapPin price={item.price} icon={MarkerIcon} />
</Marker>
);
})}
</MapView>
</View>
);
Map Pin:
const MapPin = ({ price, icon }: Props) => (
<View style={styles.bubbleContainer}>
<View style={styles.bubbleContent}>
<icon />
<Text style={styles.bubblePrice}>{price}</Text>
</View>
<View style={styles.bubbleArrow} />
</View>
);
My data is:
const staticData = [
{
name: 'test station',
type: 'ShellIcon',
price: '15.00p',
coordinates: {
latitude: 52.56504897473243,
longitude: -1.9835459043698045,
},
},
{
name: 'test station',
type: 'BpIcon',
price: '10.00p',
coordinates: {
latitude: 52.564655445785036,
longitude: -1.9895132194946084,
},
},
{
name: 'test station',
type: 'TexacoIcon',
price: '12.00p',
coordinates: {
latitude: 52.56675992926686,
longitude: -1.9925531724706291,
},
},
];
2
Answers
I believe what you need is the mapping:
You can’t just use the
string
value for it to obtain the variable from theimport
(Do note thatmarker.type
is type ofstring
whereas what you expecting inicon
is something constructable like a SVG icon Component)I think it is because you initializing
marker
in the map but you try to reach the value usingitem
.Also, move your icon to
staticData
like below:And change
MapPin
too:If you do not want to do it like this you need to write switch case in order to pass MarkerIcon by using type from staticData.