Skip to content

Instantly share code, notes, and snippets.

@shanecp
Created September 26, 2022 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecp/5bd1a53d94446249ccf6a89d78f2950e to your computer and use it in GitHub Desktop.
Save shanecp/5bd1a53d94446249ccf6a89d78f2950e to your computer and use it in GitHub Desktop.
Swift Location Distance Demo
import CoreLocation
struct Venue {
var name: String
var location: CLLocation
init(name: String, lat: Double, lng: Double) {
self.name = name
self.location = CLLocation(latitude: lat, longitude: lng)
}
}
class LocationsService {
var locations: Array<Venue>
init() {
locations = []
locations.append(Venue(name: "New York", lat: 40.712775, lng: -74.005973))
locations.append(Venue(name: "Manhattan", lat: 40.78306, lng: -73.971249))
locations.append(Venue(name: "Brooklyn", lat: 40.678178, lng: -73.944158))
locations.append(Venue(name: "San Francisco", lat: 37.77493, lng: -122.419415))
locations.append(Venue(name: "Bayview Opera House", lat: 37.735187, lng: -122.390002))
// ...
// add more locations to demo
// get geo-codes from https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
}
// this method is used to demonstrate filtering a lot of geo-locations based on a given lat,lng and a radius
// often this will be a query to a backend server, and the backend will do the filtering using a more efficient formula
// for this demo, we use a list of known locations in memory. The backend will often return the distance
func getLocations(lat: Double, lng: Double, radius: Double) -> Array<Venue> {
let radiusInMeters = radius * 1000
let userLocation = CLLocation(latitude: lat, longitude: lng)
var response = [Venue]()
if (locations.count > 0) {
for i in locations {
// you may also return the distance from current location, so it can be used for sorting
// sorting is not included in this demo
if (i.location.distance(from: userLocation) < radiusInMeters) {
response.append(i)
}
}
}
return response
}
}
let locationsService = LocationsService()
// here we give the user's starting location. Usually the user's current geo-location
// the radius is given in km. This will filter the locations with a given raidus.
// var locations = locationsService.getLocations(lat: 40.712775, lng: -74.005973, radius: 500)
// usage demo
// get locations closer to New York
print("Locations closer to New York")
var locations = locationsService.getLocations(lat: 40.712775, lng: -74.005973, radius: 500)
if (locations.count > 0) {
for i in locations {
print(i.name)
}
} else {
print("No Locations found")
}
// get locations closer to San Francisco
print("Locations closer to San Francisco")
locations = locationsService.getLocations(lat: 37, lng: -122, radius: 500)
if (locations.count > 0) {
for i in locations {
print(i.name)
}
} else {
print("No Locations found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment