I have been playing a lot with react native lately an now I am trying to draw a polygon on map with a lot of coordinates.
My JSON (something like this)
{
"routeGeoJSON":
{
"features":
[
"geometry":
{
"coordinates": [
[
[ 122, 12 ], [ 211, 19 ], ... //[ lon, lat ]
]
]
}
]
}
}
Now I want to push the value of coordinates to a new array.
My code so far
//import function
class RouteScreen extends Component {
constructor(props) {
super(props);
let polygon = [];
let routePolygon = this.props.routes.data.routeGeoJSON.features[0].geometry.coordinates[0];
let obj = {};
obj.latitude = routePolygon.forEach((element) => {
polygon.push(element[1]);
});
obj.longitude = routePolygon.forEach((element) => {
polygon.push(element[0]);
});
this.state = { polygon };
render() {
const { polygon } = this.state;
return (
<View style={styles.container}>
<MapView
provider={this.props.provider}
style={styles.map}
zoomEnabled
initialRegion={{
latitude: this.props.routes.data.routeGeoJSON.features[0].geometry.coordinates[0][0][1],
longitude: this.props.routes.data.routeGeoJSON.features[0].geometry.coordinates[0][0][0],
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
>
<MapView.Polygon
coordinates={polygon}
fillColor="rgba(0, 200, 0, 0.5)"
strokeColor="rgba(0,0,0,0.5)"
strokeWidth={2}
/>
// some code
</MapView>
// some code...
I want to fetch the coordinates from the JSON and push it to the polygon array so it will dynamically become like this
polygon: [
{
latitude: 12,
longitude: 122
},
{....}, {...}....
]
But this resulting in error while updating property 'coordinates' of a view managed by: AIRMapPolygon null expected Map, got a double
What did I missed here? Is this the correct way to use forEach with push?
Thank you for your help and suggestion.
foreachis small case notforEachtypeerror: undefined is not a function(evaluating 'routePolygon.foreach(function(element){ polygon.push(element[1])})