React Google Maps模块 - 如何处理GeoJSON MultiPolygons

时间:2016-06-12 08:24:09

标签: google-maps reactjs geojson

所以我一直在使用google-maps-react example for converting GeoJSON data to polygons。我遇到的问题是该示例不支持转换'MultiPolygon'类型的GeoJSON功能。 (多个多边形组合在一起)。

有什么我可以在示例中更改以支持它吗?我想我可以在以下函数中添加一个案例:

function geometryToComponentWithLatLng(geometry) {
  const typeFromThis = Array.isArray(geometry);
  const type = typeFromThis ? this.type : geometry.type;
  let coordinates = typeFromThis ? geometry : geometry.coordinates;

  switch (type) {
    case 'Polygon':
      return {
        ElementClass: Polygon,
        paths: coordinates.map(
          geometryToComponentWithLatLng, {
            type: 'LineString'
          }
        )[0],
      };
    case 'LineString':
      coordinates = coordinates.map(
        geometryToComponentWithLatLng, {
          type: 'Point'
        }
      );
      return typeFromThis ? coordinates : {
        ElementClass: Polyline,
        path: coordinates,
      };
    case 'Point':
      coordinates = {
        lat: coordinates[1],
        lng: coordinates[0]
      }
      return typeFromThis ? coordinates : {
        ElementClass: Marker,
        ChildElementClass: InfoWindow,
        position: coordinates,
      };
    default:
      throw new TypeError('Unknown geometry type: ${ type }');
  }
}

1 个答案:

答案 0 :(得分:1)

通过在“MultiPolygon'”的开关中添加一个案例来管理自己解决这个问题,然后对“多边形”的案例稍作修改,如下所示:

switch (type) {
case 'MultiPolygon':
  return {
    ElementClass: Polygon,
    paths: coordinates.map(
      geometryToComponentWithLatLng, {
        type: 'Polygon'
      }
    ),
  };
case 'Polygon':
  coordinates = coordinates.map(
    geometryToComponentWithLatLng, {
      type: 'LineString'
    }
  )[0];
  return typeFromThis ? coordinates : {
    ElementClass: Polygon,
    path: coordinates,
  };
case 'LineString':
  coordinates = coordinates.map(
    geometryToComponentWithLatLng, {
      type: 'Point'
    }
  );
  return typeFromThis ? coordinates : {
    ElementClass: Polyline,
    path: coordinates,
  };
case 'Point':
  coordinates = {
    lat: coordinates[1],
    lng: coordinates[0]
  }
  return typeFromThis ? coordinates : {
    ElementClass: Marker,
    ChildElementClass: InfoWindow,
    position: coordinates,
  };
default:
  throw new TypeError('Unknown geometry type: ${ type }');
}