โดยพื้นฐานแล้ว เมื่อฉันกดปุ่ม ฟังก์ชันของฉันจะเริ่มฟังการเปลี่ยนแปลงพิกัดและเชื่อมต่อข้อมูลที่รวบรวมใหม่เข้ากับอาร์เรย์
เมื่อกดปุ่มอีกครั้ง Listener จะถูกลบออก ผู้ใช้สามารถกดปุ่มและเริ่มฟังพิกัดชุดใหม่อีกครั้ง พวกเขาสามารถทำซ้ำได้
this.state = {
pathArray: [], // originally a 1D array, should become 2D as user keep using the app
pathCount: 0,
}
นี่คือลักษณะที่อาร์เรย์สุดท้ายควรมีลักษณะเพื่อให้ฉันสามารถดำเนินการส่วนการเรนเดอร์ต่อไปได้ ฉันต้องการที่จะสามารถสร้างดัชนีใหม่ในอาร์เรย์แบบไดนามิกเพื่อให้ข้อมูลใหม่ถูกผลักไปที่ด้านล่างของรายการ
ฉันจะสร้างอาร์เรย์ใหม่ภายในอาร์เรย์รูทได้อย่างไรเมื่อกดปุ่ม และต่อท้ายวัตถุในอาร์เรย์ลูกจนกว่าจะกดปุ่มอีกครั้ง หากกดปุ่มอีกครั้ง อาร์เรย์ลูกใหม่จะถูกสร้างขึ้นอีกครั้ง และกระบวนการจะดำเนินต่อไปเรื่อยๆ
pathArray = [
[
{ lat: 0, long: 0 },
{ lat: 1, long: 2 },
],
[
{ lat: 0, long: 0 },
{ lat: 1, long: 2 },
],
]
onButtonPressed = () => {
const { running } = this.state
if (!this.state.running) {
this.followWatchCoords() // start collecting data
} else {
this.stopFollowing() //stop collecting data
}
}
followWatchCoords = () => {
this.followUserWatchID = navigator.geolocation.watchPosition(({ coords }) => {
this.calculateDistance(coords)
}, null, GEOLOCATION_SETTINGS)
}
calculateDistance = (coords) => {
const { latitude, longitude } = coords
const positionLatLong = pick(coords, ['latitude', 'longitude'])
let newArr = this.state.pathArray.slice(); //copy array to mutate
newArr.push(positionLatLong)
this.setState({
pathArray: newArr,
})
}
pathArray.push([])
หากต้องการผนวกองค์ประกอบในอาร์เรย์ที่สร้างขึ้นใหม่นี้:pathArray[pathArray.length - 1].push({lat: 1, long: 2})
- person abhishekkannojia   schedule 30.05.2017