![](/img/trans.png)
[英]How do I set custom locations based on latitude and longitude in Node.js?
[英]how do i select the longitude and latitude to return specific locations javascript
好的,我有来自 xml 文件的数据并将其解析为一个 json 对象,如下所示
1884 {
FHRSID: { '$t': '184564' },
LocalAuthorityBusinessID: { '$t': 'PI/000200989' },
BusinessName: { '$t': 'Zorba Grill Restaurant' },
BusinessType: { '$t': 'Restaurant/Cafe/Canteen' },
BusinessTypeID: { '$t': '1' },
AddressLine1: { '$t': '42 Market Place' },
AddressLine2: { '$t': 'Willenhall' },
AddressLine3: { '$t': 'Walsall' },
AddressLine4: { '$t': 'West Midlands' },
PostCode: { '$t': 'WV13 2AA' },
RatingValue: { '$t': '4' },
RatingKey: { '$t': 'fhrs_4_en-GB' },
RatingDate: { '$t': '2019-11-21' },
LocalAuthorityCode: { '$t': '433' },
LocalAuthorityName: { '$t': 'Walsall' },
LocalAuthorityWebSite: {
'$t': 'http://www.walsall.gov.uk/index/environment/food_production_and_quality.htm'
},
LocalAuthorityEmailAddress: { '$t': 'environmentalhealth@walsall.gov.uk' },
Scores: {
Hygiene: { '$t': '0' },
Structural: { '$t': '0' },
ConfidenceInManagement: { '$t': '10' }
},
SchemeType: { '$t': 'FHRS' },
NewRatingPending: { '$t': 'False' },
Geocode: {
Longitude: { '$t': '-2.05513900000000' },
Latitude: { '$t': '52.58389000000000' }
}
我想选择某个经度和某个纬度之间的范围,但我只能设法将一个值或所有值打印到控制台,我的代码如下所示
fs.readFile( "./FHRS433en-GB.xml", function(err, data) {
const xmlObj = xmlParser.toJson(data, {reversible: true, object: true})
const estDetail = xmlObj["FHRSEstablishment"]['EstablishmentCollection']['EstablishmentDetail']
const objProps = (obj) => {
for (let val in obj) {
console.log(val, obj[val]);
};
};
for (let i=0;i < estDetail.length;i++) {
let lonGitude = estDetail[i]['Geocode']['Longitude'];
let laTitude = estDetail[i]['Geocode']['Latitude'];
if (laTitude <= 52.5876183 && Latitude >= 52.5756329) {
// console.log(estDetail);
}
}
objProps(estDetail);
});
我不知道如何在输出具有 Latitude: { '$t': '52.58389000000000' } 时选择我想要的信息,我不确定它是否会改变我对数据的调用方式。 任何事情都会有帮助
我也有 require(fs) 和 (xml2json) 作为旁注
如果未在$t
键中进行其他配置, xml2json
将写入文本属性。 您还应该使用parseFloat
来转换字符串。
for (let i = 0; i < estDetail.length; i++) {
let longitude = parseFloat(estDetail[i]["Geocode"]["Longitude"]["$t"]);
let latitude = parseFloat(estDetail[i]["Geocode"]["Latitude"]["$t"]);
if (latitude <= 52.5876183 && latitude >= 52.5756329) {
console.log(estDetail);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.