using openlayers to get location from map.
get coordinates like this
[32.80144214630127, 39.96892690658569]
need to seperate them into latitude and longitude to post
function savetodb() {
// get array of all features
var featureArray = drawSource.getFeatures()
// Define geojson format
var geogJONSformat = new ol.format.GeoJSON()
// Use method to convert feature to geojson
var featuresGeojson = geogJONSformat.writeFeaturesObject(featureArray)
// Array of all geojson
var geojsonFeatureArray = featuresGeojson.features
for (i = 0; i < geojsonFeatureArray.length; i++) {
console.log(geojsonFeatureArray[i].geometry.coordinates)
}
posting line
$.post({
url: "https://localhost:44330/Products",
data: {
"name": "string",
"latitude": 0,
"longitude":0
}
})
couldnt figure it out how to put these coordinates into latitude and longitude lines
Just map the array elements to an object then use it with $.post
var featureArray = drawSource.getFeatures()
var geogJONSformat = new ol.format.GeoJSON()
var featuresGeojson = geogJONSformat.writeFeaturesObject(featureArray)
var geojsonFeatureArray = featuresGeojson.features
const keys = ['latitude', 'longitude']
let data = Object.assign(...geojsonFeatureArray
.map((el, i) => ({ [keys[i]]: el.geometry.coordinates })))
$.post({
url: "https://localhost:44330/Products",
data: data
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.