lovebing / react-native-baidu-map

Baidu Map SDK modules and views for React Native(Android & iOS), support react native 0.61+. 百度地图 React Native 模块,支持 react native 0.61+,已更新到最新的百度地图SDK版本。
MIT License
847 stars 322 forks source link

针对 Overlay 相关组件增加、删除遇见的一些问题 #326

Closed onlyling closed 4 years ago

onlyling commented 4 years ago

需求说明

  1. 在地图上点击一下添加一个 Marker,点击已存在的 Marker 删除它。
  2. 两个 Marker 用线连接。

基础代码

import React from "react";
import { View } from "react-native";
import { MapView, Overlay } from "react-native-baidu-map";

const getPointKey = l => `${l.latitude}_${l.longitude}`;

class Demo extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      markLocations: []
    };
  }

  onMapClick = e => {
    this.setState({
      markLocations: this.state.markLocations.concat(e)
    });
  };

  onMarkerClick = ({ position }) => {
    this.setState({
      markLocations: this.state.markLocations.filter(
        l =>
          l.latitude !== position.latitude || l.longitude !== position.longitude
      )
    });
  };

  render() {
    const { markLocations } = this.state;

    return (
      <MapView
        style={{ flex: 1 }}
        onMapClick={this.onMapClick}
        onMarkerClick={this.onMarkerClick}
      >
        {markLocations.map(_location => {
          return (
            <Overlay.Marker
              key={getPointKey(_location)}
              location={_location}
              icon={require("./img/marker-drag.png")}
            />
          );
        })}

        {/* 如果这里不套一个 View,初次打开页面会报错,大概是 null 不能是子组件 */}
        {/* 优化一下? */}
        <View>
          {markLocations.length >= 2 ? (
            <Overlay.Polyline
              lineWidth={6}
              strokeColor="FF727cf5"
              points={markLocations}
            />
          ) : null}
        </View>
      </MapView>
    );
  }
}

问题/反馈

只有 Marker

添加/删除看起来没有问题

MarkerPolyline 共存

一直添加显示正常。

删除会有问题:

问题一:

  1. 在地图上点击四下,有四个 Marker,两个之间按顺序通过 Polyline 连接起来;
  2. 删除最后一个 Marker 地图上会显示倒数第二个 Marker 消失,最后一个保留,Polyline 显示正常,后面三个点连起来的;
  3. 删除地图上显示的倒数第一个点(逻辑数据上倒数第二个),正确消失,上面最后一个点依旧显示,Polyline 显示正常,

问题二:

  1. 在地图上点击四下,有四个 Marker,两个之间按顺序通过 Polyline 连接起来;
  2. 删除倒数第二个点,所有的 Marker 都还在,Polyline 全部消失;
  3. 再次点击添加 Marker 能添加,Polyline 不再显示;

问题三:

偶尔在删除 Marker 的时候回触发红屏保存。

问题四:

const getIntermediatePoint = (a, b) => {
  return {
    longitude: (a.longitude + b.longitude) / 2,
    latitude: (a.latitude + b.latitude) / 2,
  };
};
<View>
  {markLocations.length >= 2 ? (
    <Overlay.Polyline
      lineWidth={6}
      strokeColor="FF727cf5"
      points={markLocations}
    />
  ) : null}
</View>

{/* 替换成下面的 */}
<View>
  {markLocations.length >= 2
    ? markLocations.map((_location, i) => {
        if (i === 0) {
          return null;
        }

        return (
          <Overlay.Marker
            key={getPointKey(_location)}
            location={getIntermediatePoint(_location, markLocations[i - 1])}
            icon={require('../../assets/bmap/marker1.png')}
          />
        );
      })
    : null}
</View>

同一个种类的 Marker 显示也有问题,可能中间某个就不见了。

设备

Raintdk commented 4 years ago

删除 Marker 是直接通过 改变 state 中的 markLocations 改变的么

onlyling commented 4 years ago

删除 Marker 是直接通过 改变 state 中的 markLocations 改变的么

是的。

lovebing commented 4 years ago

最新版已修复这些问题 注意 Overlay 不要用 View 包裹,Polyline 的属性有所调整

{markLocations.length >= 2 ? (
    <Overlay.Polyline
        stroke={width: 6, {color: 'FF727cf5'}}
        points={markLocations}
      />
) : <View />}