简体   繁体   中英

how do i select the longitude and latitude to return specific locations javascript

Okay, i have data from and xml file and have parsed it into a json object which looks like this

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' }
}

i would like to select a range between a certain longitude and a certain latitude but i can only manage to print one value or all values to the console, my code looks like this

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);

});

i dont know how to go about selecting the information i want when the output has Latitude: { '$t': '52.58389000000000' } in front im not sure if it changes how i am meant to call the data. anything would be helpful

also i have require(fs) and (xml2json) just as a side note

xml2json writes the text attributes if not configured otherwise in $t keys. You should also use parseFloat to convert the strings.

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);
  }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM