繁体   English   中英

如何通过 ID 访问 react-native-maps 标记

[英]How to access react-native-maps marker by ID

我一直在做一些研究,但我不太清楚如何根据我给它的 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());
  };

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM