Project 78, E-commerce and business
Store Locator
Help people find the nearest shop. It sorts by real distance, shows which ones are open right now and needs no map library or API key.
- Main API
- Geolocation
- Distance
- Haversine formula
- Dependencies
- None
- Your browser
- Checking
Find a store
Use your location or pick an area. Stores are sorted by distance and marked open or closed. Click a store to see it on the map.
Showing all stores. Share your location to sort by distance.
How it works
- Where are you?The Geolocation API asks for permission and returns latitude and longitude. If people say no, they can pick an area instead.
- Real distanceThe haversine formula works out the distance between two points on a sphere, which is accurate enough for finding a nearby shop.
- A map without a libraryStore positions are scaled from latitude and longitude into an SVG box. Swap in a map library later if you need street detail.
function haversine(lat1, lng1, lat2, lng2) {
const R = 6371, r = Math.PI / 180; // km
const a = Math.sin((lat2 - lat1) * r / 2) ** 2 +
Math.cos(lat1 * r) * Math.cos(lat2 * r) * Math.sin((lng2 - lng1) * r / 2) ** 2;
return 2 * R * Math.asin(Math.sqrt(a));
}
navigator.geolocation.getCurrentPosition(({ coords }) => {
stores.sort((a, b) => haversine(coords.latitude, coords.longitude, a.lat, a.lng)
- haversine(coords.latitude, coords.longitude, b.lat, b.lng));
});