Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps React谷歌地图模块-如何处理GeoJSON多多边形_Google Maps_Reactjs_Geojson - Fatal编程技术网

Google maps React谷歌地图模块-如何处理GeoJSON多多边形

Google maps React谷歌地图模块-如何处理GeoJSON多多边形,google-maps,reactjs,geojson,Google Maps,Reactjs,Geojson,所以我一直在使用谷歌地图。我遇到的问题是,该示例不支持转换“MultiPolygon”类型的GeoJSON特性。(多个多边形分组在一起) 在这个例子中有什么我可以改变的吗?我想我可以在下面的函数中添加一个case: function geometryToComponentWithLatLng(geometry) { const typeFromThis = Array.isArray(geometry); const type = typeFromThis ? this.type : g

所以我一直在使用谷歌地图。我遇到的问题是,该示例不支持转换“MultiPolygon”类型的GeoJSON特性。(多个多边形分组在一起)

在这个例子中有什么我可以改变的吗?我想我可以在下面的函数中添加一个case:

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

通过为“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 }');
}