React MapBox:缩放上移动的标记

发布于 2025-01-11 22:36:22 字数 1000 浏览 0 评论 0原文

这个问题已经被问过很多次了,但似乎没有一个解决方案能够解决问题。

当我缩小时,所有标记都会移出位置。

这是我的 App.js:

import * as React from "react";
import Map, { Marker } from "react-map-gl";

function App() {
  return (
    <Map
      initialViewState={{
        longitude: -100,
        latitude: 40,
        zoom: 3.5,
      }}
      mapStyle="mapbox://styles/mapbox/streets-v9"
      mapboxAccessToken={process.env.REACT_APP_MAPBOX}
      style={{ width: "100vw", height: "100vh" }}
    >
      <Marker longitude={-100} latitude={40} anchor="bottom">
        <img src="./pin.png"/>
      </Marker>
    </Map>
  );
}

export default App;

这是 index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import 'mapbox-gl/dist/mapbox-gl.css';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

没有额外的样式

This question has been asked many times already, but none of the solutions seem to solve the problem.

When I zoom out, all my markers move out of position.

Here is my App.js:

import * as React from "react";
import Map, { Marker } from "react-map-gl";

function App() {
  return (
    <Map
      initialViewState={{
        longitude: -100,
        latitude: 40,
        zoom: 3.5,
      }}
      mapStyle="mapbox://styles/mapbox/streets-v9"
      mapboxAccessToken={process.env.REACT_APP_MAPBOX}
      style={{ width: "100vw", height: "100vh" }}
    >
      <Marker longitude={-100} latitude={40} anchor="bottom">
        <img src="./pin.png"/>
      </Marker>
    </Map>
  );
}

export default App;

and here is index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import 'mapbox-gl/dist/mapbox-gl.css';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

There is no extra styling

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2025-01-18 22:36:22

我遇到了同样的问题,这让我头疼了好几天......但我通过添加此导入“mapbox-gl/dist/mapbox-gl.css”修复了我的问题。尝试一下,看看是否有效。它应该有效。呵呵...

I had the same issue and it gave me headache for days...but I fixed mine by adding this import 'mapbox-gl/dist/mapbox-gl.css'. Try it and see if it works. It should work. Hehe...

兔小萌 2025-01-18 22:36:22

我遇到了同样的问题,我按照以下步骤修复:

  1. 转到您的 index.js
  2. 添加 import 'mapbox-gl/dist/mapbox-gl.css';

您可以检查以下链接:
https://docs.mapbox.com/help /tutorials/use-mapbox-gl-js-with-react/

I had the same issue and I fix following these steps:

  1. Go to your index.js
  2. Add import 'mapbox-gl/dist/mapbox-gl.css';

You can check the following link:
https://docs.mapbox.com/help/tutorials/use-mapbox-gl-js-with-react/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文