javascript - 显示一些标记导致其他标记消失

标签 javascript reactjs google-maps react-leaflet

我正在尝试编写一个应用程序来显示我所在城市的车站和电车的当前位置。每隔 5 秒,我在 componentDidMount() 方法中从 API 获取数据,并将其作为 props 传递给我的 map 组件。我第一次显示 map 时,每个标记都在其位置。然而,5 秒后,我的停靠站标记消失了,只有电车标记就位。

我已经尝试在不同的地方获取,模拟而不是获取电车数据。此外,无论我使用 Google map 还是 react-leaflet,结果都是一样的。

应用程序.js:

class App extends Component {

constructor() {
    super();
    this.state = {};
    this.getStops = this.getStops.bind(this);
    this.getTrams = this.getTrams.bind(this);
}

componentDidMount() {
    this.getStops();
    this.getTrams();
    setInterval(() => this.getTrams(), 5000);
}

getStops() {
    fetch('http://localhost:8080/stopInfo/stops')
    .then(response => response.json())
    .then(stops => this.setState({ stops: stops.stops }));
}

getTrams() {
    fetch('http://localhost:8080/vehicleInfo/vehicles')
    .then(response => response.json())
    .then(trams => this.setState({ trams: trams.vehicles }));
}

render() {

    function normalizeMarker(obj) {

      if (obj.latitude !== undefined && obj.longitude !== undefined) {
        obj.latitude /= (1000 * 3600);
        obj.longitude /= (1000 * 3600);
      }

      return obj;

    }

    if (this.state.stops === undefined) {
      return ('loading stops...');
    }

    if (this.state.trams === undefined) {
      return ('loading trams...');
    }

    let trams = this.state.trams.map((tram) => normalizeMarker(tram));
    let stops = this.state.stops.map((stop) => normalizeMarker(stop));

    return (
      <div className="App">
        <MapContainer stops={stops} trams={trams}/>
      </div>
    );
}

MapContainer.js:

export default class MapContainer extends Component {

constructor(props) {
    super(props);
    this.state = {
        lat: 50.0613888889,
        lng: 19.9383333333,
        zoom: 13
    };
}

displayStop(stop) {
    return (
        <Marker key={stop.id} position={[stop.latitude, stop.longitude]} icon={stopIcon}>
            <Popup>
                Stop: {stop.name}
            </Popup>
        </Marker>
    );
}

displayTram(tram) {

    if (tram.deleted === true) {
        return;
    }

    return (
        <Marker key={tram.id} position={[tram.latitude, tram.longitude]} icon={tramIcon}>
            <Popup>
                Tram: {tram.name}
            </Popup>
        </Marker>
    );
}

render() {

    const position = [this.state.lat, this.state.lng];

    return (
        <Map center={position} zoom={this.state.zoom}>
            <TileLayer
                attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            />
            {this.props.stops.map((stop) => this.displayStop(stop))}
            {this.props.trams.map((tram) => this.displayTram(tram))}
        </Map>
    );
}

我花了很多时间寻找解决方案。如果有人能帮助我,那就太好了。

最佳答案

我已经弄清楚了 - 我每次渲染时都在调用 normalizeMarker() 函数。对于有轨电车,这会起作用,因为它们每 5 秒更新一次,但停靠点只保存一次状态,因此对它们来说,它被称为多次扰乱坐标。

无论如何,感谢您的回答!我会研究它们,以便改进我的代码:)

关于javascript - 显示一些标记导致其他标记消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156188/

相关文章:

reactjs - handleSubmit react 钩子(Hook)形式不起作用,它不执行 onSumbmit

google-maps - Google Map V3,如何获取多个目的地的最佳行驶路线列表?

android - 旋转 Google map 中的两层标记图标

javascript - 获取页面源码

reactjs - 为什么不直接从 Action Creator Redux 调度到 store 呢?

javascript - Angular ng-change 或 ng-click 无法选择

javascript - 未捕获的类型错误 : Cannot call a class as a function in react and redux

node.js - 使用 Node.js 显示 Google map

javascript - 你能调用一个在调用者上下文中返回的函数吗?

Javascript 贪吃蛇游戏(防止逆转,同时保持响应能力)