Google maps React谷歌地图模块-如何处理GeoJSON多多边形
所以我一直在使用谷歌地图。我遇到的问题是,该示例不支持转换“MultiPolygon”类型的GeoJSON特性。(多个多边形分组在一起) 在这个例子中有什么我可以改变的吗?我想我可以在下面的函数中添加一个case: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
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 }');
}