Calculating walking distances in iOS - ios

I have a list of latitude and longitude values in a database. Based on a user’s location I need to extract the closest location from the database based on walking distances. For this purpose, I need to first get all values from the database into an array and then calculate WALKING distances to each and then return the lat/long of the shortest distance.
Note: I have no problem extracting the database values. I need help with the calculation of walking distances between 2 points.
I have so far found threads here which either show straight line distances. Is there any way to get walking distances between 2 points? If so, I would appreciate if you could point me in the right direction (link/previous question) or provide some guidance on what classes or functions to use.

You can get the distance in meter using the
CLLocationDistance distance = [locationOne distanceFromLocation:locationTwo];
you can acoordingly convert it into CentiMeter or Kilometer, this may serve your purpose

Related

Avoiding Triangulation of User Location Hacks

I am building a location based app where users can see distance to other users. What decimal place should I round longitude and latitude to to avoid making it possible for users to triangulate the position of other users? I am not sure what a "reasonable level of uncertainty" for other user's position is.
Round Long/Lat to vary degrees of decimal places to introduce error. Hundredths is a good answer, since on average long/lat lines are .69 miles apart, thus introducing an average .69x.69 square mile error.
You can also use a custom geohashing system to build your own designated shapes to lump points into. Geohashes are better near the poles because they don't risk their area going to zero as the system with long/lat does because latitude lines have a distance of close to zero near the poles, which potentially poses a security risk there for triangulation.
At the end of the day, for a consumer app that nobody is going to use at the poles, rounding seems sufficient.

Calculate angle between three GPS coordinates

perhaps this is a simple question.
I have 3 GPS coordinates (one is the current user location). What I want now is to calculate the angle between the user location and the two GPS coordinates. Imagine the user location in the center of the two other points, the three points can be seen as a triangle. And I want to calculate the angle at the user location.
I hope someone can help me because I have no idea how to do this with spherical coordinates like the GPS coordinates I have.
THX - nekro
For short distances (less than 100km, say) you can safely ignore the spherical nature of the calculation and treat the problem as a 2 cartesian coordinate problem. For large distances the spherical geometry gets pretty gnarly. I could probably figure it out, but I don't want to think that hard right now.
Edit:
All you need to do is to convert both coordinates to KM, and then treat it as a cartesian problem. (At a small scale, you can ignore the curved nature of the "lines" and treat them as normal cartesian grid lines, since the curvature is small enough to ignore at that scale)
The distance per degree of latitude is constant. The distance for a degree of longitude changes based on latitude.
Do a google search on "KM per degree of longitude" and find a link that explains it clearly. Here's one: http://www.colorado.edu/geography/gcraft/warmup/aquifer/html/distance.html
You could use thessien polygons and calculate the geometry on those from a strictly GIS perspective. If you have qgis or arcgis this should be fairly simple. These packages offer APIs which might suit your needs.
You're essentially doing two calculations (bearing to (or from) current position to two other positions) and not crosstrack (distance from a great circle line between to other points).
However, both can be found in Ed William's Aviation Formulary which has the most comprehensive collection of formulas for spherical calculations I've found.
You would be looking for "Course between points" which is listed as:
tc1=mod(atan2(sin(lon1-lon2)*cos(lat2),
cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon1-lon2)), 2*pi)

How to determine which regions, each defined by longitude , latitude and a radius, contain a given location?

I have a large data set of regions , each of which is defined by a longitude, latitude and a given radius. I have a location point with a a latitude and longitude and I need to determine which of the regions contains my point.
Currently I am using brute force : I compute the distance between the target point and each region's center longitude and latitude ; if the distance is less than the radius of the region, I include the region in in my result.
Obviously this solution is not tenable.
Can GeoHash be used to formulate a solution ?
Edit : The business problem is given a set of cell phone with known ranges and a fixed set of available locations owned by a real estate holding, where is the place for a signal repeater. There are other considerations of course besides location and distance. Otherwise someone will have drive around the country with a signal detection kit -- not optimal . Not a homework question. I have Comp Sci background but GIS is new to me and I am willing to learn.
Edit : I will continue using brute force across several ec2 instances. Not the most optimum solution but it works. Thank you all for proposed solutions but unfortunately given the time and knowledge constraints and vagueness of the methodology, I am not going to be able to try them out.
The usual approach is to use a spatial index like quad tree or kd tree.
To this index you add the rectangular bounds of all circles to build up the index.
Uisng the quad tree:
Query the index which objects overlap a quad node at given position. The result will be some circles , these you check as you described.
quad trees don't like the deletion of elements.

How does the distanceFromLocation method work?

I frequently use the distanceFromLocation method for CLLocation objects to get their distance from other locations. Enumerating through an array of CLLocations, I then compare each to my reference location using this method.
I'm curious to know the processing/memory implications for using distanceFromLocation, especially for a large number of CLLocation objects in succession. How does this method work - does it connect to the server to get the data, or does it calculate the distance based on some mathematical formula, such as the Haversine Formula?
Is there a more efficient method to compare distances between 1 reference location and an array of CLLocation objects?
They are likely are using the Spherical Law of Cosines instead of the Haversine (why? see this question).
If all you want to do is compare many points against one point to see which is closest, then maybe you don't care about the accuracy of the computed distance and just about performance. In that case perhaps using Pythagoras' theorem would work for you.
All of these algorithms are detailed on this web page, which says in part:
If performance is an issue and accuracy less important, for small
distances Pythagoras’ theorem can be used on an equirectangular
projection:*
You could implement a function using Pythagoras' theorem then benchmark it against the one in CLLocation and against my implementation of distanceInMetersFromRadians that uses the Spherical Law of Cosines to see how much performance difference there is.
From the documentation:
distanceFromLocation:
This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations.
So yes, I assume it is using the Haversine Formula (or a modification of it).
Have you used Instruments and measured it? Until you have done, it's pointless.
You can take shortcuts. Let's say you want the nearest point. Find a formula that gives you roughly the right result. Usually there's a square root involved, so get a formula for the square of the distance - that's quicker and works just as well. Find the nearest point with your formula. Now say the nearest point is 178.96 meters apart according to your formula. You can then check all points that are say less than 180 meters away with the exact formula.
For small distances and on iOS 9, I have found that the values obtained by distanceFromLocation are reliably close to those found by the Vincenty formula using the WGS-84 ellipsoid. In my experience, they are accurate within about 7 or 8 significant figures.
A spherical model, such as the law of cosines or the Haversine formula, does not compare well for small distances.
For more information, see the geopy documentation and a table of values for comparison.

Calculating distance between two coordinates given that I have drawn my own route

I have seen people asking questions about calculating distance between two geo points and that distance would be the direct line between two points while taking into consideration earth's radius and using the formulas that people have suggested.
In my case I have drawn a bus route on Google map (I am using iOS and kml file that contains the coordinates) and I want to calculate the distance between two bus stops on that route and I want to use the route coordinates that I have described my self. My bus routes are closed loop and there are no shortcuts or multiple ways to get to a point.
Any suggestions how to do that?
thank you,

Resources