ASP.NET Core下如何将OpenLayers获取的GeoJSON坐标拆分经纬度用jQuery提交
GeoJSON 标准中坐标数组的默认顺序为 [经度, 纬度],赋值时注意不要颠倒顺序。
修改后的完整代码如下:
function savetodb() { // 获取所有绘制的要素数组 var featureArray = drawSource.getFeatures() // 定义GeoJSON格式化工具 var geogJONSformat = new ol.format.GeoJSON() // 将要素转换为GeoJSON对象 var featuresGeojson = geogJONSformat.writeFeaturesObject(featureArray) // 提取所有GeoJSON要素数组 var geojsonFeatureArray = featuresGeojson.features for (let i = 0; i < geojsonFeatureArray.length; i++) { // 提取当前要素的坐标 const coordinates = geojsonFeatureArray[i].geometry.coordinates const longitude = coordinates[0] const latitude = coordinates[1] // 提交请求到后端 $.post({ url: "https://localhost:44330/Products", data: { "name": "string", // 此处替换为实际业务值 "latitude": latitude, "longitude": longitude } }).done(function(response) { // 可自定义提交成功逻辑 console.log("提交成功", response) }).fail(function(error) { // 可自定义提交失败逻辑 console.error("提交失败", error) }) } }
场景适配说明
- 如果你的OpenLayers地图使用默认的EPSG:3857(Web墨卡托投影),拿到的坐标不是WGS84标准经纬度,需要增加坐标转换步骤,坐标提取部分修改为:
// 先将墨卡托坐标转换为WGS84经纬度 const wgs84Coordinates = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326') const longitude = wgs84Coordinates[0] const latitude = wgs84Coordinates[1] - 若要素数量较多,可先将所有坐标组装为数组后批量提交到后端,减少HTTP请求次数。
内容的提问来源于stack exchange,提问作者berkay
相关产品推荐
相关产品推荐

