Search near by people by using Latitude & longitude IOS - 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)

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
)

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

Geo points in area

I am working on iOS app where at some point I want to get user's location and present her all point of interests on map that are inside a circular area where centre of this area is user's current location and radius is constant. Points of interests are stored in database with their coordinates (latitude, longitude).
I have already managed to get user's location. Now I am trying to figure out how to calculate if certain coordinates are in that area.
I was thinking that I can calculate distance of some point from centre using this equation:
d = sqrt((centre_latitude - point_latitude)^2 + (centre_longitude - point_longitude)^2)
Where d is distance of that point from circle centre. Then I could simply compare d with radius.
I am not sure if this is right and also efficient approach. I can imagine that if I have thousands of points this would be really slow (query database for each point then do the math).
You can try this:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
float lon= // longitude you want to compare to your current postion
float lat=//your latitude you want to compare to your current position
float rad=//radius , if you give this 100m, then it checks the given points are within the 100m from you are not
CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:lat
longitude:lon];
CLLocation *lastLocation=[locations lastObject];
//display current lat and lon in text fields
currentLat.text=[NSString stringWithFormat:#"%f",lastLocation.coordinate.latitude];
currentLon.text=[NSString stringWithFormat:#"%f",lastLocation.coordinate.longitude];
CLLocationDistance distance = [lastLocation distanceFromLocation:centerLocation];
if (distance<=rad) {
// you are within the radius
}
CLLocationAccuracy accuracy = [lastLocation horizontalAccuracy];
if(accuracy <=10) { //accuracy in metres
[manager stopUpdatingLocation];
}
}
You can use Haversine formula.
I have it implemented in Java for Android, maybe it helps you.
private static double calculateDistanceInMiles(Location StartP, Location EndP) {
//Haversine formula
//double Radius=6371; // to get distance in kms
double Radius=3963.1676; //to get distance in miles
double lat1 = StartP.getLatitude();
double lat2 = EndP.getLatitude();
double lon1 = StartP.getLongitude();
double lon2 = EndP.getLongitude();
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}

iOS - ID of nearby Places with Google Places API export to database

In my app I need find nearby places from my current location and export ID these places to my database. It's all. I use this code below with Google Places API and then I see marked nearby places from my current location on the map. Then I want to get ID these places. How can I get this?
- (IBAction)pickPlace:(UIButton *)sender {
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(51.5108396, -0.0922251);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001);
GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast coordinate:southWest];}
You seem to have copied Googles example code for their place picker. Is that correct? You can not get results from the placekicker. However if you always want to get places around the user, you can look at this, specifically "currentPlaceWithCallback:". The list you get back should be a list of GMSPlaces around you (also containing the ID).
If that is not good enough or if you want to be able to search around another location there is this web API

Using the Wikimapia.org Api

I am trying to use the wikimapia api for finding venues of specific cities or places given their longitude and latidute.
There isn't much of some documentation, but I suppose that it would just be an http request.
Now, the problem I have is about the specific ulr. I tried this one:
http://api.wikimapia.org/?function=search
&key= myKey
&q=lat= theLatidute
&lon= theLongitude
&format=json
but it doesn't seem to work. Any help will be appreciated..
The search API requires that you set a search location (long and lat) as well as the name of something to search for that location.
For example, to find a train station near a particular coordinate:
http://api.wikimapia.org/?function=search&key=[key]&q=Train+Station&lat=[latitude]&lon=[longitude]&format=json
If you're just trying to find a list of objects that are close to a coordinate, without a search term, you need to use the box API with small offsets:
http://api.wikimapia.org/?function=box&key=[key]&lon_min=[lon_min]&lat_min=[lat_min]&lon_max=[lon_max]&lat_max=[lat_max]&format=json
If you only want to input one set of coordinates, you can compute lon_min, lon_max, lat_min and lat_max like this:
// 1 degree latitude is roughly 111km, 0.001 degrees lat is about 100m
var lat_min = latitude - 0.001;
var lat_max = latitude + 0.001;
// 1 degree longitude is not a static value
// it varies in terms of physical distance based on the current latitude
// to compute it in meters, we do cos(latitude) * 111000
var meters_per_longdeg = Math.cos((3.141592 / 180) * latitude) * 111000;
// then we can work out how much longitude constitutes a change of ~100m
var range = 100 / meters_per_longdeg;
var long_min = longitude - range;
var long_max = longitude + range;

Resources