Search code examples
javascriptreact-nativereact-native-maps

How to draw polyline on react native maps with many points(latitude and longitude)?


hi i want to draw lines between multi locations get from API

this is my data from API

(3) [{…}, {…}, {…}]
0:
CONTACTPERSONID: 2017011300
ENABLEPOLL: 0
HARDWAREID: 409792319815
LANDMARKID: 0
MESSAGETIME: "2دقيقه پيش"
MOVINGSTATE: "m"
NICKNAME: "ایران 78 - 875 ع 73"
POSDESCRIPTION: "ايران: استان كرمان - شهربابك"
SENTDATE: "18:14"
SENTDATE1: "1398-06-23 18:14:16"
SIGNATURE: "1-Normal"
SPEED: 51
TRUCKSTATE: "در حال حرکت"
VEHICLETYPE: 0
XPOINT: 55.13055
YPOINT: 30.128971
__proto__: Object
1: {HARDWAREID: 420474797787, NICKNAME: "رضا نوری پور ایران 62 - 374 ع 66", SENTDATE: "18:05", XPOINT: 51.2906383, YPOINT: 35.6798033, …}
2: {HARDWAREID: 2225434572, NICKNAME: "عابدین پور 938ع43", SENTDATE: "17:16", XPOINT: 48.33547, YPOINT: 38.26992, …}

lat and long in Xpoint and Ypoint

this is my code to show :

<View style={{ height: hp('50%'), width: wp('100%') }}>
            <MapView
              provider={PROVIDER_GOOGLE}
              onLayout={this.onMapLayout}
              style={styles.containerMap}
              initialRegion={{
                latitude: this.props.data ? this.props.data[0].YPOINT : '',
                longitude: this.props.data ? this.props.data[0].XPOINT : '',
                latitudeDelta: 0.0922,
                longitudeDelta: 0.0421,
              }} >
              {this.state.isMapReady && this.props.data.map((value, index) => {
                let poly = {
                  latitude: value.YPOINT,
                  longitude: value.XPOINT,
                }
                return < Polyline
                strokeColor='#fd016f'
                coordinates={poly}
                />
              })
              }

            </MapView>

          </View>

but get this error : **Error while updating property 'coordinates'of a view managed by:AIRMapPolyline **


Solution

  • that code Logical mistake because that code create some polyLines with one point in this code below is true : i change my code and work fine :

     <View style={{ height: hp('50%'), width: wp('100%') }}>
                <MapView
                  provider={PROVIDER_GOOGLE}
                  onLayout={this.onMapLayout}
                  style={styles.containerMap}
                  initialRegion={{
                    latitude: this.props.data ? this.props.data[0].YPOINT : '',
                    longitude: this.props.data ? this.props.data[0].XPOINT : '',
                    latitudeDelta: 0.0922,
                    longitudeDelta: 0.0421,
                  }} >
                  {this.state.isMapReady && <Polyline
                    strokeWidth={2}
                    strokeColor="red"
    
                    coordinates={[...this.props.data.map((value, index) => { return { latitude: value.YPOINT, longitude: value.XPOINT } })]}
                  />}
                </MapView>
    
              </View>