Skip to content

Commit 7b145c2

Browse files
committed
Added notes to what has been tried.
1 parent 5c6efd1 commit 7b145c2

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/utils/location-utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const haversineDistance = (lat1: number, lon1: number, lat2: number, lon2: numbe
3838
return R * c;
3939
};
4040

41+
// find the nearest location nearest the lat/long. We need this if using the citiesJSON service
42+
// because this service returns all the cities in the bounding box, not just the nearest
4143
const findNearestCity = (targetLat: number, targetLon: number, citiesArray: any) => {
4244
let nearestCity = null;
4345
let minDistance = Infinity;
@@ -57,36 +59,41 @@ const findNearestCity = (targetLat: number, targetLon: number, citiesArray: any)
5759
export const geoLocSearch = async (lat: number, long: number, bounds: IBounds) => {
5860
const userClause = `username=${kGeonamesUser}`;
5961
const locClause = `lat=${lat}&lng=${long}`;
60-
const boundsClause = `north=${bounds.north}&south=${bounds.south}&east=${bounds.east}&west=${bounds.west}`;
6162
const maxRowsClause = `maxRows=50`;
6263
const radiusClause = `radius=25`;
6364
const populationLimitClause = "city15000";
65+
// boundsClause is used if using citiesJSON service
66+
const boundsClause = `north=${bounds.north}&south=${bounds.south}&east=${bounds.east}&west=${bounds.west}`;
6467
// const url = `${kGeolocCitiesService}?${[locClause, userClause, boundsClause, maxRowsClause].join("&")}`;
6568
const url = `${kGeolocService}?${[locClause, userClause, maxRowsClause, radiusClause, populationLimitClause ]
6669
.join("&")}`;
6770
try {
6871
const response = await fetch(url);
6972
if (response.ok) {
7073
const data = await response.json();
71-
// filter out geonames where population is 0
74+
// There is a bug in the findNearbyPlaceNameJSON service that returns 0 population places
75+
// even when city15000 population filter is set. So we filter out geonames where population is 0
7276
const geonames = data.geonames.filter((geoname: any) => {
7377
return geoname.population > 0;
7478
});
7579
console.log("GeoLocSearch geonames", JSON.parse(JSON.stringify(geonames)));
7680
console.log("data size", geonames.length);
81+
// Sort geonames by population in descending order to try to get the nearest most populated city
7782
const sortedGeonamesByPopulation = geonames.sort((a: any, b: any) => {
7883
const populationA = parseInt(a.population, 10);
7984
const populationB = parseInt(b.population, 10);
8085
return populationB - populationA; // Sort in descending order
8186
});
8287
console.log("sorted geonames", JSON.parse(JSON.stringify(sortedGeonamesByPopulation)));
8388

84-
// find the nearest location nearest the lat/long
89+
// find the nearest location nearest the lat/long. We use the commented out code if using the citiesJSON service
8590
// const nearest = findNearestCity(lat, long, data.geonames);
8691
const nearest = sortedGeonamesByPopulation[0];
8792
console.log("nearest location", JSON.parse(JSON.stringify(nearest)));
8893
return nearest
8994
? {success: true, values: {location:`${nearest.name}, ${nearest.adminCode1}`}}
95+
// Return this value if using the citiesJSON service.
96+
// citiesJSON service does not have adminCode1 (state name), se we use countrycode
9097
// ? {success: true, values: {location:`${nearest.name}, ${nearest.countrycode}`}}
9198
: {success: false, values: {location: "Unknown Location"}};
9299
} else {

0 commit comments

Comments
 (0)