我正在编写一个用于车辆跟踪的应用程序。在 Google Maps API 的帮助下,我可以获取路线并提取所有需要的信息。Elevations API 响应出现了问题。从 DirectionRender 类中,我将路径和距离作为道具发送。GM Elevations 请求是通过 lift.getElevationAlongPath(option,PlotElevation) 完成的。PlotElevation (elevations,status) 是一个回调函数。但是,无论我如何尝试仅从它那里接收一个响应(使用 useMemo、useEffect,我想我都尝试了所有方法),仍然存在重新呈现响应的问题。OVER_QUERY_LIMIT 或无休止的重新渲染。有人可以帮忙吗?谢谢
const Elevation = React.memo(props =>{
const [path, setPath] = useState({...props.path})
const [distance, setDistance]=useState({...props.distance})
const [elevationArray, setElevationArray] = useState(null)
const [stop, setStop] = useState(false)
let pathElev = JSON.parse(JSON.stringify(path))
React.useEffect(() => {
setPath(props.path)
}, [props.path])
React.useEffect(() => {
setDistance(props.distance)
}, [props.distance])
let elevator = new window.google.maps.ElevationService;
let numberSamples = parseInt( distance/40)
let options = {
'path':path,
'samples':numberSamples
}
//The problem starts here
const PlotElevation = (elevations, status) => {
if (stop === false){
console.log('status',status)
console.log(JSON.parse(JSON.stringify(elevations)))
setElevationArray(elevations)
//setStop(true)
console.log(elevations[19].elevation)
return
}
}
const Memo = React.useMemo(
()=>{
elevator.getElevationAlongPath(
options,PlotElevation
)
},[elevator.getElevationAlongPath(
options,PlotElevation
)])
// elevator.getElevationAlongPath(
// {
// path: path,
// samples: 100
// }, elevations =>{
// setElevationArray({
// // We’ll probably want to massage the data shape later:
// // elevationArray: elevations
// })
// }
// )
return (
<div>
{Memo}
{console.log('path is received ', pathElev)}
{console.log('number of samples', numberSamples)}
{console.log('elevation check ',elevationArray)}
{/* {elevator.getElevationAlongPath(
options,PlotElevation)} */}
</div>
)
})
导出默认高程