我目前有两种成功的方法来基于我的数据API显示地图标记。此代码在地图上显示所有地图标记:
/// Working Code Option 1 (No Filters) ///
this.state = {
filterCrime: '',
posts: []
}
{this.state.posts.map((user, index)=> {
return (
<MapView.Marker key={user.ID} coordinate={{latitude: user.lat, longitude: user.lng}} />
);
})}
此代码基于filterCrime值成功过滤了标记,并且有效。
/// Working Code Option 2 (With Category Filters) ///
this.state = {
filterCrime: '',
posts: []
}
{this.state.posts.map((user, index)=> {
if(user.category == this.state.filterCrime ){
return (
<MapView.Marker key={user.ID} coordinate={{latitude: user.lat, longitude: user.lng}} />
);
}})}
我的问题是:如果filterCrime值为空,我也要显示显示所有地图标记的Option 1,否则,如果filterCrime状态具有一个值,我想显示选项2并仅显示过滤的标记。
答案 0 :(得分:2)
所以只要有条件就可以使用过滤器。
{this.state.posts.map((user, index)=> {
if ( !this.state.filterCrime || user.category == this.state.filterCrime ) {
return <MapView.Marker key={user.ID} coordinate={{latitude: user.lat, longitude: user.lng}} />
}
return null
})}
答案 1 :(得分:0)
在使用地图之前对其进行过滤。
form.get('controlname').setValue(res.id);