-
-
Save sk-chanch/c8d12eceeff52f1f881461bfc235a279 to your computer and use it in GitHub Desktop.
Return a cluster of random locations around a given location
This file contains 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
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