嗨,我正在尝试从一组对象创建一个geojson对象。我正在使用$.each()
遍历数组并尝试返回一个名为kendofeatures(geojson对象)的对象数组,如下所示:
kendofeatures.type = "Feature";
kendofeatures.properties = {
"id": _id,
"Id": _id,
"FaultType": _faulttype,
"EntryGUID": _entryguid,
"Date": _datcaptured,
"Status": _status,
"Area": _area,
"Zone": _zones,
"Latitude": _latutude,
"Longitude": _longitude,
"Comments": _comments,
"User": _fullname,
"UserID": _idnumber,
"UserPhone": _phonenumber,
"UserMail": _email
};
kendofeatures.geometry = {
"type": "Point",
"coordinates": [
_latutude,
_longitude
]
};
我使用的代码看起来像这样:
function displayFilterResults() {
var kendofeatures = {};
var dataSource = $("#grid").data("kendoGrid").dataSource;
console.log(dataSource);
// TODO:Gets the filter from the dataSource
var filters = dataSource.filter();
console.log(filters);
// TODO:Gets the full set of data from the data source
var allData = dataSource.data();
console.log(allData);
// TODO:Applies the filter to the data
var query = new kendo.data.Query(allData);
console.log(query);
var filteredData = query.filter(filters).data;
console.log(filteredData);
var geoJsonObject = {
"type": "FeatureCollection",
"features": []
};
$(filteredData).each(function (index, item) {
var _area = item.Area;
var _email = item.C_Email;
var _fullname = item.C_FullName;
var _idnumber = item.C_IDNumber;
var _phonenumber = item.C_MobileNumber;
var _comments = item.Comments;
var _datcaptured = item.DateTimeCaptured;
var _faulttype = item.FaultType;
var _id = item.Id;
var _entryguid = item.RecordID;
var _reportedvia = item.ReportedVia;
var _status = item.Responce;
var _latutude = item.XCoordinate;
var _longitude = item.YCoordinate;
var _zones = item.Zone;
kendofeatures.type = "Feature";
kendofeatures.properties = {
"id": _id,
"Id": _id,
"FaultType": _faulttype,
"EntryGUID": _entryguid,
"Date": _datcaptured,
"Status": _status,
"Area": _area,
"Zone": _zones,
"Latitude": _latutude,
"Longitude": _longitude,
"Comments": _comments,
"User": _fullname,
"UserID": _idnumber,
"UserPhone": _phonenumber,
"UserMail": _email
};
kendofeatures.geometry = {
"type": "Point",
"coordinates": [
_latutude,
_longitude
]
};
console.log(kendofeatures);
geoJsonObject.features.push(kendofeatures);
});
console.log(geoJsonObject);
}
而不是filteredData
中具有不同对象属性的数组,featureCollection
导致具有相同属性,几何等的同一对象的数组。
我的问题是如何返回所需的featureCollection
。这样我的geoJsonObject
可以看起来像:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": 1,
"Id": 1,
"FaultType": "Sewer Burst",
"EntryGUID": "0ectey-yu78ee7e-01",
"Date": "2016/04/01",
"Status": "Assigned",
"Area": "Senga",
"Zone": "Residential",
"geometry": {
"type": "Point",
"coordinates": [
24.960937499999996,
11.178401873711785
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
30.585937499999996,
-19.642587534013032
]
}
},
{
"type": "Feature",
"properties": {
"id": 2,
"Id": 2,
"FaultType": "Water Burst",
"EntryGUID": "0ectey-yu78yre7e-01",
"Date": "2016/02/01",
"Status": "Closed",
"Area": "CBD",
"Zone": "CBD",
},
"geometry": {
"type": "Point",
"coordinates": [
24.43359375,
-9.535748998133627
]
}
},
{
"type": "Feature",
"properties": {
"id": 3,
"Id": 3,
"FaultType": "Pot Hole",
"EntryGUID": "07yye54y-yu78ee7e-01",
"Date": "2016/03/21",
"Status": "Unassigned",
"Area": "Riverside",
"Zone": "Residential",
},
"geometry": {
"type": "Point",
"coordinates": [
45.439453125,
-17.476432197195518
]
}
}
]
}
filteredData
看起来像这样:
[
{
Area: "Senga"
C_Email: "tkmawarire@live.com"
C_FullName: "Takudzwa Kain Mawarire"
C_IDNumber: "29-279485 B 27"
C_MobileNumber: "+263779960053"
Comments: "Main hole burst near the far end"
DateTimeCaptured: Fri Apr 15 2016 01: 14: 40 GMT + 0200(South Africa Standard Time)
FaultType: "Sewer Burst"
Id: 1
RecordID: "a63c8656-437c-4d7a-9fe3-dad5edf73e08"
ReportedVia: "Web Application"
Responce: "Assigned"
XCoordinate: 29.8395538
YCoordinate:
-19.51572
Zone: "Institution"
},
{
Area: "Senga"
C_Email: "tkmawarire@live.com"
C_FullName: "Takudzwa Kain Mawarire"
C_IDNumber: "29-279485 B 27"
C_MobileNumber: "+263779960053"
Comments: "Sewer burst near football pitch"
DateTimeCaptured: Fri Apr 15 2016 23: 58: 40 GMT + 0200(South Africa Standard Time)
FaultType: "Sewer Burst"
Id: 2
RecordID: "c776808c-3f09-4221-99e6-11a44bcec692"
ReportedVia: "Web Application"
Responce: "Unassigned"
XCoordinate: 29.8392315
YCoordinate:
-19.5195084
Zone: "Institution"
}, {
Area: "Senga"
C_Email: "tkmawarire@live.com"
C_FullName: "Takudzwa Kain Mawarire"
C_IDNumber: "29-279485 B 27"
C_MobileNumber: "+263779960053"
Comments: "Water bleading from the ground"
DateTimeCaptured: Sat Apr 16 2016 01: 18: 08 GMT + 0200(South Africa Standard Time)
FaultType: "Water Pipe Burst"
Id: 7
RecordID: "aff0ece0-b084-4031-8cc8-43ca66f6adb5"
ReportedVia: "Web Application"
Responce: "Closed"
XCoordinate: 29.8388252
YCoordinate:
-19.5002327
Zone: "Institution"
}
]
答案 0 :(得分:0)
将featureCollection = [];
移到每个循环之外。您希望在循环之前以空白数组开始,然后在迭代期间将对象推送到该数组。
var featureCollection = [];
$(filteredData).each(function(index, item) {
var kendofeatures = {};
// ...
featureCollection.push(kendofeatures);
});
var geoJsonObject = {
"type": "FeatureCollection",
"features": featureCollection
};
或者,首先定义geoJsonObject
并直接在循环中使用它:
var geoJsonObject = {
"type": "FeatureCollection",
"features": []
};
$(filteredData).each(function(index, item) {
var kendofeatures = {};
// ...
geoJsonObject.features.push(kendofeatures);
});