title: Algorithm - grouping locations on map
body: <p>I have a list of n locations, each consists of a latitude, longitude and a time stamp. These locations will be pinned on the map. </p>
  
  <p>However, it is required to group locations that are close together, with the most recently changed location as centre, so that the map will not be flooded by pins.</p>
  
  <p>My initial thoughts would be: </p>
  
  <ol>
  <li>Sort the locations by time stamp </li>
  <li>Select the latest location</li>
  <li>Calculate the distance to the latest location for the n-1 locations</li>
  <li>Select those locations within the radius, say, 5km, then remove them from the list </li>
  <li>Repeat steps 2-4</li>
  </ol>
  
  <p>This method works but it is highly inefficient. Worst case would be ~O(n^2). </p>
  
  <p>Are there any algorithms to perform better?</p>