Last active
December 18, 2024 03:01
-
-
Save zzdoreen/2996a02fd6544157160936b998fa83b0 to your computer and use it in GitHub Desktop.
百度地图处理geojson数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function displayGeoJSON(geoJsonData) { | |
if (!geoJsonData || !geoJsonData.features) { | |
console.error("无效的GeoJSON数据"); | |
return; | |
} | |
geoJsonData.features.forEach(function (feature) { | |
var geometry = feature.geometry; | |
var coordinates = []; | |
var type = geometry.type; | |
if (type === "Point") { | |
coordinates = [geometry.coordinates[1], geometry.coordinates[0]]; // [lng, lat] | |
var point = new BMap.Point(coordinates[0], coordinates[1]); | |
var marker = new BMap.Marker(point); | |
map.addOverlay(marker); | |
} else if (type === "LineString") { | |
coordinates = geometry.coordinates.map(function (coord) { | |
return new BMap.Point(coord[0], coord[1]); | |
}); | |
var polyline = new BMap.Polyline(coordinates, { strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5 }); | |
map.addOverlay(polyline); | |
} else if (type === "Polygon") { | |
coordinates = geometry.coordinates[0].map(function (coord) { | |
return new BMap.Point(coord[0], coord[1]); | |
}); | |
var polygon = new BMap.Polygon(coordinates, { strokeColor: "transparent", strokeWeight: 0, strokeOpacity: 0, fillColor: "rgba(0, 0, 255, 0.1)", fillOpacity: 0.5 }); | |
map.addOverlay(polygon); | |
} else if (type === "MultiPoint") { | |
coordinates = geometry.coordinates.map(function (coord) { | |
return new BMap.Point(coord[0], coord[1]); | |
}); | |
coordinates.forEach(function (coord) { | |
var marker = new BMap.Marker(coord); | |
map.addOverlay(marker); | |
}); | |
} else if (type === "MultiLineString") { | |
geometry.coordinates.forEach(function (line) { | |
var lineCoordinates = line.map(function (coord) { | |
return new BMap.Point(coord[0], coord[1]); | |
}); | |
var polyline = new BMap.Polyline(lineCoordinates, { strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5 }); | |
map.addOverlay(polyline); | |
}); | |
} else if (type === "MultiPolygon") { | |
geometry.coordinates.forEach(function (polygon) { | |
var polygonCoordinates = polygon[0].map(function (coord) { | |
return new BMap.Point(coord[0], coord[1]); | |
}); | |
var poly = new BMap.Polygon(polygonCoordinates, { strokeColor: "transparent", strokeWeight: 0, strokeOpacity: 0, fillColor: "rgba(0, 0, 255, 0.1)", fillOpacity: 0.5 }); | |
map.addOverlay(poly); | |
}); | |
} else { | |
console.warn("不支持的GeoJSON几何类型: " + type); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment