Moving on from the question stated on How do you center the map on a clicked symbol using MapBox and React Native?, I've been trying to setState of my activeID (the id of my selected feature) from my geoJson. Aka, when I select an Icon with a a specific feature.id, how do I set state to that icon's feature.id. In my code below, activeID is undefined. Can anyone tell me what I might be doing wrong? Thanks!
UPDATE: I tried to console.log feature.id (see in code below) and I get undefined. I am for some reason not able to reach it.
Sample feature:
{ "type": "FeatureCollection", "features": [ { "id": 1, "type": "Feature", "properties": { "name": "Hotel" }, "geometry": { "type": "Point", "coordinates": [ 12.584664, 55.680532 ] } }
constructor(props) {
super(props);
this.state = {
activeIndex: 0,
activeID: -1,
this.onPress = this.onPress.bind(this);
this.onActiveIndexChange = this.onActiveIndexChange.bind(this);
async onPress(pressFeature) {
const { screenPointX, screenPointY } = pressFeature.properties;
const hitFeatureCollection = await this.map.queryRenderedFeaturesAtPoint(
[screenPointX, screenPointY],
null,
['singleIcon']
);
let feature = null;
let currentFeature = null;
if (hitFeatureCollection.features.length > 0) {
feature = hitFeatureCollection.features[0];
for (let i = 0; i < this.props.featureCollection.features.length; i++) {
currentFeature = this.props.featureCollection.features[i];
console.log(feature.id);
if (feature.id === currentFeature.id) {
this.setState({
activeIndex: i,
isChangeFromPress: true,
destination: feature.geometry.coordinates
});
break;
}
}
}
onActiveIndexChange(index) {
const feature = this.props.featureCollection.features[index];
if (!feature) {
return;
}
this.setState({
activeIndex: index,
activeID: feature.id,
isChangeFromPress: false,
destination: feature.geometry.coordinates
});
}
console.log(feature)
– cb64feature.properties.id
instead. – cb64id
isn't a property that is listed for features but is a property for layers and source. Although you would assume that ID should be an available property for mostly any object, it might not be here. – cb64