【问题标题】:How to access react-native-maps marker by ID如何通过 ID 访问 react-native-maps 标记
【发布时间】:2021-03-28 20:52:53
【问题描述】:

我一直在做一些研究,但我不太清楚如何根据我给它的 id 获取 react-native-maps 标记的坐标。但是,当我运行代码时,出现“未定义不是对象”错误

这是代码

  const getBounds = () => {
    if (mapRef === null) {
      return;
    }
    mapRef
      .getMapBoundaries()
      .then((res) => {
        console.log(res);
      })
      .catch((err) => console.log(err));
      console.log("Marker" , mapRef.markers["MyID"].showCallout());
  };

【问题讨论】:

    标签: reactjs react-native react-native-maps


    【解决方案1】:

    这不是你使用 ref 的方式。 如果标记是 MapView 的直接子级,则需要在 .current.props.children 数组中搜索标记。如果它们是平坦的,它将位于数组索引 0 上,只需使用 console.log 打印内容即可找出您的确切子结构。 大致如下:

    mapRef.current.props.children[0].find(marker => marker.id = "MyID");
    

    另一方面,为什么不重用 key 道具?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      相关资源
      最近更新 更多