Erick Maynard Erick Maynard - 1 year ago 81
Swift Question

How To Tell If User Is At A Specific Location?

Essentially what I need to do is find out if a user is at a specific place (IE at a venue). And if the user is, allow access to a specific ViewController.

I've been looking high and low for an answer to this problem online and surprisingly I haven't found anything. I will say I'm pretty new to iOS development.

I don't need anything as complex as geofencing like in the Ray Wenderlich tutorial, and I don't need to run it in the background. I also don't need to know if they entered or left. Just whether or not they are within that area or not when the user clicks a button.

I've gotten as far as being able to get the users location using CoreLocation, but I'm confused as to how I will go about identifying if the user is at the specific location. Ideally, I will want a radius of about 5 miles (It's a big location).

Answer Source

if you have the user's location as well as the venue's location you can do the following:

let radius: Double = 5 // miles

let userLocation = CLLocation(latitude: 51.499336, longitude: -0.187390)
let venueLocation = CLLocation(latitude: 51.500909, longitude: -0.177366)

let distanceInMeters = userLocation.distanceFromLocation(venueLocation)
let distanceInMiles = distanceInMeters * 0.00062137

if distanceInMiles < radius {
    // user is near the venue
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download