Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

给 new BMap.Marker 对象添加的 click 监听器报错 b.ma(...).Ub is not a function #267

Open
xyxc0673 opened this issue Apr 15, 2022 · 4 comments

Comments

@xyxc0673
Copy link

xyxc0673 commented Apr 15, 2022

示例代码

const { setContainer, map } = useMap({
  enableScrollWheelZoom: true,
});
const marker = new BMap.Marker(
  { lat: 32.3360520521223, lng: 119.1827553809459 },
);
marker.setTitle(device.serialNumber);
marker.addEventListener('click', function onclick() {
  console.log('onClick', marker.getPosition());
  map.centerAndZoom(marker.getPosition(), 18);
});
marker.enableDragging();
map.addOverlay(marker);

在初始化完成,点击 marker 报错,但是移动后再次点击却没有问题。

VM1736:1 
        
       Uncaught TypeError: b.ma(...).Ub is not a function
    at HTMLSpanElement.eval (eval at m_ (getscript?v=3.0&ak=xfl48OtZTyuYT44Ocm1GsEO02feR5nPu&services=&t=20220328195528:1:79720), <anonymous>:1:1311)
@jaywcjlove
Copy link
Member

@xyxc0673
Copy link
Author

@jaywcjlove
Copy link
Member

@xyxc0673 我使用 useMarker,看上去没有报错,我不太清楚是什么原因报错的,你可以参考我的封装里面怎么写,找到原因可以告诉我

https://codesandbox.io/embed/uiwjs-react-baidu-map-issues-267-usemarker-7sd2cx?fontsize=14&hidenavigation=1&theme=dark

Example Code
import ReactDOM from "react-dom";
import { useRef, Fragment, useEffect } from "react";
import { Provider, APILoader, useMap, useMarker } from "@uiw/react-baidu-map";

const Example = () => {
  const divElm = useRef(null);
  const { setContainer, map } = useMap({
    widget: ["GeolocationControl", "NavigationControl"],
    zoom: 8
  });
  const { setType, marker } = useMarker({
    map,
    position: { lng: 121.444017, lat: 31.237787 }
  });
  useEffect(() => {
    if (divElm.current && !map) {
      setContainer(divElm.current);
    }
  });
  useEffect(() => {
    if (marker) {
      console.log("marker::", marker);
      marker.addEventListener("click", () => {
        console.log("marker", marker);
      });
    }
  }, [marker]);
  return (
    <Fragment>
      <button onClick={() => setType("red2")}>设置 red2</button>
      <button onClick={() => setType("loc_blue")}>设置 loc_blue</button>
      <button
        onClick={() =>
          marker.setPosition(new BMap.Point(121.497197, 31.232847))
        }
      >
        设置坐标点
      </button>
      <button onClick={() => marker.setAnimation(2)}>设置动画</button>
      <button onClick={() => marker.setAnimation(null)}>取消动画</button>
      <div ref={divElm} style={{ height: "100%" }} />
    </Fragment>
  );
};

const Demo = () => (
  <div style={{ width: "100%", height: "300px" }}>
    <APILoader akay="GTrnXa5hwXGwgQnTBG28SHBubErMKm3f">
      <Provider>
        <Example />
      </Provider>
    </APILoader>
  </div>
);
ReactDOM.render(<Demo />, document.getElementById("container"));

@xyxc0673
Copy link
Author

@jaywcjlove 如果我查出了原因的话和你说一下,不过我现在的需求是需要遍历添加 marker 以及获得 Map 的 ref 来调用方法,useMarker 的话,好像不太方便遍历,然后使用 Map + Marker 组件的话,通过 ref 的方式获取的 Map 的实例是有的,但是调用方法时没有反应。
示例代码:https://codesandbox.io/s/react-baidu-map-demo-forked-3itzw7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants