typeerror:在react表单中调用函数以在单击时缩放(mapboxgl)

hgb9j2n6  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(246)

我有一张搜索表。当我单击中的“搜索”按钮时 SearchForm.js 文件中,我希望我的Map缩放到搜索结果的边界框中。
然而,我不断地得到一份工作 TypeError: Cannot read property 'addEventListener' of null 在我的 Map.js 文件
以下是这篇文章的摘录 SearchForm.js 文件我在react表单中有一个materialui按钮。

  1. // some code omitted for example
  2. return (
  3. <div
  4. className={classes.root}
  5. >
  6. <Form
  7. onSubmit={onSubmit}
  8. initialValues={initialValues}
  9. validate={validate}
  10. render={({ handleSubmit, values }) => (
  11. <form onSubmit={handleSubmit} noValidate>
  12. // some code omitted for example
  13. <Button
  14. id='searchButton'
  15. variant="contained"
  16. color="primary"
  17. endIcon={<SearchIcon></SearchIcon>}
  18. type="submit"
  19. fullWidth
  20. >
  21. Search
  22. </Button>
  23. // some code omitted for example
  24. </form>
  25. )}
  26. />
  27. </div>
  28. );

这是你的电话号码 Map.js 文件我的价值观 maxWest , maxSouth , maxEast ,及 maxNorth 由于我目前正在处理ui,所以暂时都是硬编码的。在顶部 Map.js 文件,我一定要导入 SearchForm.js 通过使用线路 import SearchForm from './SearchForm.js'; .

  1. var northEast = [131.308594, 46.195042];
  2. var southEast = [117.597656, 8.233237];
  3. var southWest = [79.101563, 32.842674];
  4. var northWest = [86.847656, 44.715514];
  5. ...
  6. var maxSouth = Math.min(southEast[1], southWest[1]);
  7. var maxWest = Math.min(southWest[0], northWest[0]);
  8. var maxNorth = Math.max(northWest[1], northEast[1]);
  9. var maxEast = Math.max(northEast[0], southEast[0]);
  10. // The error is from the addEventListener()*******************
  11. document.getElementById('searchButton').addEventListener('click', function () {
  12. map.fitBounds([
  13. [maxWest - 5, maxSouth - 5], // southwestern corner of the bounds
  14. [maxEast + 5, maxNorth + 5] // northeastern corner of the bounds
  15. ]);
  16. });

我不确定我遗漏了什么,我也不明白为什么我会出现这个错误。非常感谢您的帮助!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题