简体   繁体   English

有没有办法在 react-native-maps 屏幕 position 上获取标记?

[英]Is there a way to get marker's on screen position in react-native-maps?

I need custom tooltip with semitransparent background which overlays the map.我需要具有覆盖 map 的半透明背景的自定义工具提示。

So in common we draw first MapView.所以一般我们先画MapView。 After marker press on top of MapView we draw overlay (backgroundColor: "#00000033"), on top of it draw tooltip and image over the marker position to simulate highlight.在 MapView 顶部按下标记后,我们绘制叠加层(backgroundColor:“#00000033”),在其顶部绘制工具提示和图像在标记 position 上以模拟突出显示。 And now I need to get absolute position of marker on screen to draw my image over it.现在我需要在屏幕上获得绝对 position 的标记以在其上绘制我的图像。 There is point prop in onPress nativeEvent regarding to docs but I haven't it in my real event and I don't know if that is what I need. onPress nativeEvent 中有关于文档的point prop,但我在真实事件中没有,我不知道这是否是我需要的。

{
   "action": "marker-press",
   "coordinate": {
      "latitude": -15.3469687,
      "longitude": 37.100195
   },
   "id": "unknown",
   "target": 295
}

Is there a way to get marker's on screen position?有没有办法在屏幕 position 上获取标记?

Yes, you can use the measureInWindow method on the ref of the custom component.是的,您可以在自定义组件的 ref 上使用measureInWindow方法。 If your custom component doesn't have this method available, you can wrap it in a View, and use the method on that.如果您的自定义组件没有此方法可用,您可以将其包装在一个视图中,然后在其上使用该方法。

Example:例子:

const markerRef = useRef();
const [markerPosition, setMarkerPosition] = useState({});

const measureMarker = () => {
  markerRef.current?.measureInWindow((x, y, width, height) => {
    const { width: screenWidth, height: screenHeight } = Dimensions.get('screen');
    // if any non-zero, on-screen values, save measurements
    if (!(x || y || width || height)) return;
    if (x > screenWidth || y > screenHeight) return;
    setMarkerPosition({x, y, width, height});
  }
};

...

return (
  <View ref={markerRef} onLayout={measureMarker}>
    <CustomMarker
      ...
);

See the docs for measureInWindow for more.有关更多信息,请参阅measureInWindow的文档。 measure would also work for the purpose of getting the on-screen position. measure也可以用于获取屏幕上的 position。

Note that these callbacks won't work until native layout is completed.请注意,在本机布局完成之前,这些回调不会起作用。

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

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