Skip to content

Instantly share code, notes, and snippets.

@sterlingsky
Created January 9, 2024 00:25
Show Gist options
  • Select an option

  • Save sterlingsky/a0578ca6b19366cae509a03e3ab87dee to your computer and use it in GitHub Desktop.

Select an option

Save sterlingsky/a0578ca6b19366cae509a03e3ab87dee to your computer and use it in GitHub Desktop.
Build a grid of lat long points to feed into a rank tracker
const {Client} = require('@googlemaps/google-maps-services-js');
const geolib = require('geolib');
const client = new Client({});
async function isWater(latitude, longitude, apiKey) {
try {
const response = await client.reverseGeocode({
params: {
latlng: [latitude, longitude],
key: apiKey,
},
});
const results = response.data.results;
return results.some((result) => result.types.includes('natural_feature'));
} catch (error) {
console.error(error);
return false;
}
}
async function generateGrid(lat, lon, distanceInMiles, gridSize, apiKey) {
const points = [];
const distanceInMeters = distanceInMiles * 1609.34;
const offset = distanceInMeters / (gridSize - 1);
for (let i = -(gridSize - 1) / 2; i <= (gridSize - 1) / 2; i++) {
for (let j = -(gridSize - 1) / 2; j <= (gridSize - 1) / 2; j++) {
const point = geolib.computeDestinationPoint({latitude: lat, longitude: lon}, offset * i, 90);
const finalPoint = geolib.computeDestinationPoint({latitude: point.latitude, longitude: point.longitude}, offset * j, 0);
if (!(await isWater(finalPoint.latitude, finalPoint.longitude, apiKey))) {
points.push(finalPoint);
}
}
}
return points;
}
// Usage Example
const apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Replace with your Google Maps API key
const centralLat = 40.7128; // Replace with your latitude
const centralLon = -74.006; // Replace with your longitude
const gridSize = 7; // Size of the grid (7x7 in this case)
generateGrid(centralLat, centralLon, 3, gridSize, apiKey).then((points) => {
console.log(points);
});
@sterlingsky
Copy link
Author

This not only builds a grid, it also queries the Google Maps API to remove points that are over bodies of water, because....like...uhm...whop the help cares how you rank over a lake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment