Skip to content

Instantly share code, notes, and snippets.

@inorganik
Created June 22, 2017 14:37
Show Gist options
  • Save inorganik/fcaa143eba6178fb672c5c335a11c564 to your computer and use it in GitHub Desktop.
Save inorganik/fcaa143eba6178fb672c5c335a11c564 to your computer and use it in GitHub Desktop.
Return a cluster of random locations around a given location
import Foundation
import CoreLocation
struct randomLocations {
// create random locations (lat and long coordinates) around user's location
func getMockLocationsFor(location: CLLocation, itemCount: Int) -> [CLLocation] {
func getBase(number: Double) -> Double {
return round(number * 1000)/1000
}
func randomCoordinate() -> Double {
return Double(arc4random_uniform(140)) * 0.0001
}
let baseLatitude = getBase(number: location.coordinate.latitude - 0.007)
// longitude is a little higher since I am not on equator, you can adjust or make dynamic
let baseLongitude = getBase(number: location.coordinate.longitude - 0.008)
var items = [CLLocation]()
for i in 0..<itemCount {
let randomLat = baseLatitude + randomCoordinate()
let randomLong = baseLongitude + randomCoordinate()
let location = CLLocation(latitude: randomLat, longitude: randomLong)
items.append(location!)
}
return items
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment