Skip to content

Instantly share code, notes, and snippets.

@sk-chanch
Forked from inorganik/randomLocations.swift
Created June 4, 2020 05:38
Show Gist options
  • Save sk-chanch/c8d12eceeff52f1f881461bfc235a279 to your computer and use it in GitHub Desktop.
Save sk-chanch/c8d12eceeff52f1f881461bfc235a279 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