polyline.tsx 1.28 KB
import * as React from "react";
import { ColorValue, ImageSourcePropType, Platform, processColor, requireNativeComponent } from "react-native";
// @ts-ignore
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";
import { LatLng } from "./types";

export interface PolylineProps {
  /**
   * 节点坐标
   */
  points: LatLng[];

  /**
   * 线段宽度
   */
  width?: number;
  /**
   * 线段的纹理
   */
  line?: ImageSourcePropType;
  /**
   * 线段颜色
   */
  color?: ColorValue;
  /**
   * 层级
   */
  zIndex?: number;

  /**
   * 多段颜色
   */
  colors: ColorValue[];

  /**
   * 是否使用颜色渐变
   */
  gradient?: boolean;

  /**
   * 是否绘制大地线
   */
  geodesic?: boolean;

  /**
   * 是否绘制虚线
   */
  dotted?: boolean;

  /**
   * 点击事件
   */
  onPress?: () => void;
}

export default class extends React.PureComponent<PolylineProps> {
  static defaultProps = { colors: [], with: 16 };

  render() {
    const props = {
      ...this.props,
      ...Platform.select({ android: { colors: this.props.colors.map(processColor) } }),
    };
    // @ts-ignore
    return <NativePolyline {...props} line={resolveAssetSource(props.line)} />;
  }
}

const NativePolyline = requireNativeComponent<PolylineProps>("AMapPolyline");