How To Tell If User Is At A Specific Location? - ios

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).

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
}

If you have the latitude and longitude of the venue, just create a CLLocation object for that and see how far the user is from that location.
// get the current user location, then...
let MinDistance = 100.0 // meters
let distance = venueLocation.distanceFromLocation(userLocation)
if distance < MinDistance {
// I'm close enough to the venue!
}

Related

Current location issue in ArcGIS Maps in iOS

I'm developing an application in which I have a map. I'm using ArcGIS SDK for showing map. Here I'm trying to show my current location when the map loads. I have passed my latitude and longitude to the x and y parameters of the ArcGIS function. This is the code for that function,
let lat = gpsLocation.location?.coordinate.latitude
let lng = gpsLocation.location?.coordinate.longitude
print(lat)
print(lng)
//zoom to custom view point
self.mapView.setViewpointCenter(AGSPoint(x: lat!, y: lng!, spatialReference: AGSSpatialReference.webMercator()), scale: 4e7, completion: nil)
self.mapView.interactionOptions.isMagnifierEnabled = true
But when I run the app it shows the wrong location in the map. It points to the wrong location. I have printed the lat and lng also. They are correct but the display of location is wrong in the map. How can I get the map to load my current location?
This is what it shows location when loads,
You're using latitude and longitude, i.e. a number of degrees (probably in WGS 1984), but then you're telling ArcGIS that they are in Web Mercator, i.e. a number of meters. That will definitely cause the behavior you see.
To fix it, simply replace webMercator() with WGS84().
Also, you are mixing up latitude and longitude. Latitude is y and longitude is x, and you have it the other way around.
In summary, replace your setViewpointCenter call with this:
self.mapView.setViewpointCenter(
AGSPoint(x: lon!, y: lat!, spatialReference: AGSSpatialReference.WGS84()),
scale: 4e7,
completion: nil
)

Swift mkmapview get zoom level, width or radius of visible map area from latitude longitude delta

I am in a confusion, on how to get a zoom level and radius of a visible area of the map (using mapkit mapview).
Here is what I am looking for (either of them or both, as needed) -
Zoom level, is to see at what level of the map is being shown to the users and with that information, I want to display the custom pins in the visible area. If zoom level is high, I need to show the actual logos of some commerce stores as pins. If zoom level is low, I need to show colored dots instead of logos.
As of now, I am using let updatedRadius = (mapView.camera.altitude)/1000 to get altitude of the camera, and if the updatedRadius value is > 25.0, I am showing colored dots. Below 25.0, I show the logos. I am doing this in regionDidChanged
Is this approach correct?
Radius, is to send it as a parameter to my REST API to fetch the list of places within that radius. When user zooms out on the map, visible area increases and so the REST API needs bigger radius to return the places covered in that area.
Ultimately, what should happen is, whenever user zooms out, then the radius changes. I need to send this changed radius to my REST to get an updated list.
What are latitude longtitude deltas, can we get radius/width of visible area using these values?
let latitudeDeltaVal = mapView.region.span.latitudeDelta
let longitudeDeltaVal = mapView.region.span.longitudeDelta
Can someone throw some light on what needs to be done please?
Since you need to call the api when the region changes you need to calculate the radius in mapView's delegate function, RegionDidChange.
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let centralLocation = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
self.centralLocationCoordinate = mapView.centerCoordinate
print("Radius - \(self.getRadius(centralLocation))")
}
func getRadius(centralLocation: CLLocation) -> Double{
let topCentralLat:Double = centralLocation.coordinate.latitude - mapView.region.span.latitudeDelta/2
let topCentralLocation = CLLocation(latitude: topCentralLat, longitude: centralLocation.coordinate.longitude)
let radius = centralLocation.distanceFromLocation(topCentralLocation)
return radius / 1000.0 // to convert radius to meters
}
To account for both landscape and portrait orientations, and/or situations where the map orientation is close to Northeast, Northwest, Southwest, Southeast, and to enclose the full screen up to the corners, one should consider both latitudeDelta and longitudeDelta:
func getRadius() -> Double{
let centralLocation = CLLocation(latitude: mapView.region.center.latitude, longitude: mapView.region.center.longitude)
let cornerOfMap = CLLocation(latitude: centralLocation.coordinate.latitude + mapView.region.span.latitudeDelta , longitude: centralLocation.coordinate.longitude + mapView.region.span.longitudeDelta)
let radius = centralLocation.distance(from: cornerOfMap)
return radius / 1000.0 // to convert radius to meters
}

Ordering Geofire results on Distance

I've been experimenting with Geofire for iOS but can't seem to find any way of returning the distance from the search position in a circle query. The GFQueryResultBlock only returns the key and position. Am I right in assuming that I have to calculate the distance myself?
Let's say I am making a nearby restaurant app with Firebase, and want to display the 20 closest restaurants to the user, ordered by how close they are. I could create a circle query and then increase the search radius via a loop until I find 20 restaurants. Then calculate the distance for each one and sort them before displaying them to the user. Is this a reasonable approach, given that a large amount of work is being done in the app to structure the data (calculating distance & sorting)?
I've noticed that javascript Geofire queries return distance from the center, but I guess the iOS and android versions are different from this.
When you are querying from Geofire, relevant results are automatically ordered by ascending order. So in order to get the distance , Im just using the distanceFromLocation function:
Here my code:
func getGeoFirePlaces(){
let geofireRef = FIRDatabase.database().reference().child("testForGeofire")
let geoFire = GeoFire(firebaseRef: geofireRef)
//geoFireRef is pointing to a firebase reference where I previously set all places' location
let userPosition = CLLocation(latitude: 32.0776067, longitude: 34.78912)
let circleQuery = geoFire.queryAtLocation(userPosition, withRadius: 2)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
//getting distance of each Place return with the callBack
let distanceFromUser = userPosition.distanceFromLocation(location)
print(distanceFromUser)
})
}
Hope this help!

Displaying MKAnnotationViews that are within the bounds of the visible mapview

I'm querying my server for a list of locations that are closest to the user based on his observable portion of the mapview.
I'm not entirely sure on how to do this, but should I send the center lat and long of the mapview's visible region and then search for results based on nearest radius?
I know that MKMapView has two properties called region and visibleMapRect. Should I use one of those, and if so, which one is more appropriate based on my question?
EDIT
I'm looking to implement functionality that's identical to the apple maps and Yelp app when you search for nearby locations and it shows you what's relevant based off the visible portion of the map view.
EDIT 2
I have seen a lot of people break up the visible portion of their mapview into quadrants, most commonly NW,NE,SW and SE. However, I'm still not entirely sure why theyr'e doing this. I would like to know the best way to query the back end, which contains a lat and long for each location, to find the locations which exist on the mapview.
I have looked everywhere on stackoverflow and I found a similar question here. However, it doesn't answer my question and there's no mention of visibleMapRect because it's over 5 years old.
Any help is tremendously appreciated.
So you would need to provide your server call with these 2 coord bottomleft and topright 4 points all together.
if lat of location > bottomleft.lat and if lat of location < topright.lat
if long of location > bottomleft.long and if long of location < topright.long
iOS Code would look like this the make server call with these four variables as parameters
fun viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Using region
var span: MKCoordinateSpan = mapView.region.span
var center: CLLocationCoordinate2D = mapView.region.center
// This is the farthest Lat point to the left
var farthestLeft = center.latitude - span.latitudeDelta * 0.5
// This is the farthest Lat point to the Right
var farthestRight = center.latitude + span.latitudeDelta * 0.5
// This is the farthest Long point in the Upward direction
var farthestTop = center.longitude - span.longitudeDelta * 0.5
// This is the farthest Long point in the Downward direction
var farthestBottom = center.longitude + span.longitudeDelta * 0.5
var SWCoord = MKCoordinateForMapPoint(farthestBottom, farthestLeft)
var NECoord = MKCoordinateForMapPoint(farthestTop, farthestRight)
// Using visibleMapRect
var mapRect = mapView.visibleMapRect
// This is the top right Coordinate
var NECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: mapRect.origin.y)
// This is the bottom left Coordinate
var SWCoord = getCoordinateFromMapRectanglePoint(mapRect.origin.x, y: MKMapRectGetMaxY(mapRect))
// Not needed but could be useful
// var NWCoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMinX(mapRect), y: mapRect.origin.y)
// var SECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: MKMapRectGetMaxY(mapRect))
}
func getCoordinateFromMapRectanglePoint(x: Double, y: Double) -> CLLocationCoordinate2D {
var mapPoint = MKMapPointMake(x, y)
return MKCoordinateForMapPoint(mapPoint)
}
If you have anymore questions just comment
EDIT 2 I have seen a lot of people break up the visible portion of their mapview into quadrants, most commonly NW,NE,SW and SE.
However, I'm still not entirely sure why theyr'e doing this. I would
like to know the best way to query the back end, which contains a lat
and long for each location, to find the locations which exist on the
mapview.
I'm not sure if this is what you're referring to but they could be using quadtrees as a way to efficiently search the locations of a huge amount of coordinates.
This post from Thoughtbot demonstrates how to use quadtrees to display thousands of place coordinates as clusters on the map and it includes gifs that very succinctly explain how it works:
A quad tree is a data structure comprising nodes which store a bucket of points and a bounding box. Any point which is contained within the node’s bounding box is added to its bucket. Once the bucket gets filled up, the node splits itself into four nodes, each with a bounding box corresponding to a quadrant of its parents bounding box. All points which would have gone into the parent’s bucket now go into one of its children’s buckets.
Source: How To Efficiently Display Large Amounts of Data on iOS Maps

Search near by people by using Latitude & longitude IOS

I am developing a chat app in iOS. In which a user can able to search near by people with a 1 kilo meter radius .
On registration i stored the latitude and longitude of every user .
Now tell me how can i search people near by user within 1 kilo meter radius ??
thanks alot
You can use Haversine formula to calculate the appropriate distance between current latitude, longitude and target latitude, longitude.
define DEG2RAD(degrees) (degrees * 0.01745327)
double currentLatitude = 40.727181; // current latitude of logged in user
double currentLongitude = -73.986786; // current longitude of logged in user
double destinationLatitude = 42.896578; // latitude of target user
double destinationLongitude = -75.784537; // longitude of target user
//convert obtained latitude and longitude to RADS.
double currentLatitudeRad = DEG2RAD(currentLatitude);
double currentLongitudeRad = DEG2RAD(currentLongitude);
double destinationLatitudeRad = DEG2RAD(destinationLatitude);
double destinationLongitudeRad = DEG2RAD(destinationLongitude);
//Haversine Formula.
distance = acos(sin(currentLatitudeRad) * sin(destinationLatitudeRad) + cos(currentLatitudeRad) * cos(destinationLatitudeRad) * cos(currentLongitudeRad - destinationLongitudeRad)) * 6880.1295896;
The distance obtained here is in kilometers.
My solution is directly you can't search users using Radios.Make following steps.
Send Latitude , Longitude and Radios information to Your Backend server using web service.
For your web service developer put query and get nearby users using your information.
Please refer following link:
Geo Distance Search with MySQL
You can compare two locations using this method
CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];
Or calculate from backend(PHP developers) to find the distance, because basically they only store all lat,longs.(i think)

Resources