2016-06-12 98 views
0

所以我一直在使用谷歌地圖反應example for converting GeoJSON data to polygons。我遇到的問題是該示例不支持轉換'MultiPolygon'類型的GeoJSON功能。 (多個多邊形組合在一起)。反應谷歌地圖模塊 - 如何處理GeoJSON MultiPolygons

有什麼我可以改變的例子來支持?想着也許我可以只的情況下,增加了如下功能:

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

管理通過增加的情況下,爲「的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 }'); 
}