Last active
April 25, 2019 07:27
-
-
Save robbeman/3212e35f75a6bd326e3773aff32c687e to your computer and use it in GitHub Desktop.
Simple coordinate sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sorting markers in rows from left to right. Because geolocation can be pretty | |
* detailed we can't just sort it as is, we need a math to make it feel natural. | |
* | |
* How this works: | |
* - Prioritise sorting on latitude (y-axis) | |
* - multiply the difference and round it this will "group" locations in rows. | |
* - 30 is an arbitrary value that felt good in my case, higher values will make tighter rows. | |
* - (Using `1` as multiplier you will see the sorting jumps long distances from left to right.) | |
* - If 0 (same row), sort by longitude, with full precision | |
* - Depending on your dataset you might still see some jumps | |
*/ | |
const sortedMakers = markers.sort((a, b) => { | |
return Math.round(b.lat * 30) - Math.round(a.lat * 30) || a.long - b.long; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment