【问题标题】:React Google Maps module - How to handle GeoJSON MultiPolygonsReact Google Maps 模块 - 如何处理 GeoJSON MultiPolygons
【发布时间】:2016-06-12 08:24:08
【问题描述】:

所以我一直在使用 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 }');
  }
}

【问题讨论】:

    标签: google-maps reactjs geojson


    【解决方案1】:

    通过在“MultiPolygon”的开关上添加一个案例,并对“Polygon”的案例进行一些细微的更改,我自己设法解决了这个问题,如下所示:

    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 }');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-12
      • 2019-10-23
      • 2021-11-17
      • 2021-03-26
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多