Calculate distance between 2 set of lon and lat - distance

As my question states that's I am looking for a function/formula that can calculate a distance between two points. Now I have looked at example and found great functions but none of them seem to work they all return 0 when I supply 2 sets of points. Basically I will need to pass the function the following (lat1,lon1,lat2,lon2) and get back the distance. From this distance I can check a check if another point is close by.
UPDATE
Okay so I am now using the following function,
BEGIN
DECLARE pi, q1, q2, q3 , roundedVal FLOAT ;
DECLARE rads FLOAT DEFAULT 0;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = COS(lon1-lon2);
SET q2 = COS(lat1-lat2);
SET q3 = COS(lat1+lat2);
SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) );
RETURN FORMAT((6371 * rads) , 1);
END
This works fine with Kilometres, but what I am looking for is meters. So I know I have the change the numbers in that function but which ones and what to. Any help ?

I have used this webiste in the past and it has worked for me. Has lots of useful formulas and gives examples in javascript.
http://www.movable-type.co.uk/scripts/latlong.html

I'd recommend you take a look at a spacial extention to MySQL.
http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html
If you don't fancy that, this blog might have some use to you:
http://zcentric.com/2010/03/11/calculate-distance-in-mysql-with-latitude-and-longitude/

Try this query
$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) *
sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180))*
cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM
'MyTable` WHERE distance >= ".$distance."

apply this on the values
double theta = src_longitude - dest_longitude;
double min_distance = (Math.sin(Math.toRadians(src_latitude)) * Math.sin(Math.toRadians(dest_latitude))) +(Math.cos(Math.toRadians(src_latitude)) * Math.cos(Math.toRadians(dest_latitude)) * Math.cos(Math.toRadians(theta)));
min_distance = Math.acos(min_distance);
min_distance = Math.toDegrees(min_distance);
min_distance = min_distance * 60 * 1.1515 * 1.609344;

Related

User is Inside or not Geofense Flutter

i am creating flutter attendance app where i want to check when user is marking attendance he/she is available in office area or not? if not show them how much distance they are away from radius.
i tried using this but not getting the appropriate result. i want to check 200m radius from lat long.
double geDistance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = sin(toRadians(lat1)) * sin(toRadians(lat2)) +
cos(toRadians(lat1)) * cos(toRadians(lat2)) * cos(toRadians(theta));
dist = acos(dist);
dist = toDegrees(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1000 * 1.609344;
///dist in meter
return dist;
}
double toRadians(double degree) {
return degree * pi / 180;
}
double toDegrees(double radian) {
return radian * 180 / pi;
}
i know geofense can resolve accurately this but i have no idea which method can help me to check user location is inside radius or not.

Check if 2 coordinates are in the same city

Basically, I have 2 LatLong values 1 for the user that keeps updating according to his current location and 1 stored on the database. How can I check if these 2 coordinates are in the same city or state.
Note: I used the google maps API https://maps.googleapis.com/maps/api/geocode/json?latlng=33.26944346171845,%2035.20463784635442&sensor=true&key=KEY for both coordinates and compared strings(administrative_area_level_2) to check if the user is in the same city or not. Is there a better way to do it in Flutter?
Thanks in advance!
I think your method is quite reasonable. If you don't want to use API, you can calculate the distance between two coordinates with the below method. Using this, you can assume that those below a certain distance are in the same city or state.
const convertToRadian = x => x * Math.PI / 180;
function getDistanceBetween(lat1, lat2, lng1, lng2) {
const R = 6378137; // Earth’s mean radius in meter
const dLat = convertToRadian(lat2 - lat1);
const dLong = convertToRadian(lng2 - lng1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(convertToRadian(lat1)) * Math.cos(convertToRadian(lat2)) * Math.sin(dLong / 2) * Math.sin(dLong / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // returns the distance in meter
}

How to get data that is all within radius of somewhere, and within the radius I'm looking for?

I am using postgres and postgis.
I have Posts which have a geometry, with an attribute visible_within_m which is how many meters from that point the Post should be shown in results.
I can find Posts within some random radius of some random points by doing ST_DWithin(geometry, ST_SetSRID(ST_Point(a, b), 4326), 10000)
However, I want to know how many Posts are visible with a radius of some random point.
How can I look up how many Posts are visible within a radius of some arbitrary point?
Is there a better way to do this?
You can calculate the distance between each point and the center of your circle. If the distance is grater than the radius then it is outside otherwise it's inside.
const EARTH_RADIUS = 6371000;
const toRad = function(num){return num*Math.PI/180};
var calculateDistance =
function(lat1, lon1, lat2, lon2){
var dLat = toRad(lat2 - lat1);
var dLon = toRad(lon2 - lon1);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRad(lat1)) *
Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var distance = EARTH_RADIUS * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return distance;
}
Instead of using a constant value for the distance, use the value stored in visible_within_m
SELECT * FROM mytable
WHERE ST_DWithin(geometry, ST_SetSRID(ST_Point(a, b), 4326), visible_within_m);
On a side note, st_dwithin with geometries uses the distance unit of the projection, so for 4326 it is a (meaningless) distance in degrees, not in meters.

iPhone: calculate distance while walking using GPS

I want to calculate the distance that users cover while walking using GPS. For example a user taps the start button and starts to walk or run than when he done he taps stop. What will be the minimum distance user has to travel to get the different lat long?
How can we do it in IPhone, asume we take Lat, long after every 0.3 sec than in the last we have a list of points?
You could do this by calculating the distance between 2 points (latitude, longitude):
(I haven't tested it):
-(double)distanceBetweenCoordinate:(CLLocationCoordinate2D)c1 andCoordinate:(CLLocationCoordinate2D)c2 {
double long1 = degreesToRadians(c1.longitude);
double lat1 = degreesToRadians(90 - c1.latitude);
double long2 = degreesToRadians(c2.longitude);
double lat2 = degreesToRadians(90 - c2.latitude);
double gamma = fabs(long1 - long2);
if (gamma > M_PI) {
gamma = 2 * M_PI - gamma;
}
double result = cos(lat2) * cos(lat1) + sin(lat2) * sin(lat1) * cos(gamma);
return acos(result) * 6366.1977; // Kilometers
};
CGFloat degreesToRadians(CGFloat degrees) {
return degrees * M_PI / 180;
};
UPDATE: Use distanceFromLocation - Calculate distance between two points instead

calculate distance between 2 coordinates iphone - best practice

I'm finishing an App in wish i need to show the user the distance between him and about 500 coordinates.
using the CLLocation method to calculate it, works well, but it takes about 1 minute in iPhone 4 to finish calculations for each location.
What is the best way to do it? Using span? Any other faster way?
Thanks all,
rui
I think Sahgal is right, here is some code, perhaps it will help you.
+(CGFloat)calculateDistanceBetweenSource:(CLLocationCoordinate2D)firstCoords andDestination:(CLLocationCoordinate2D)secondCoords
{
// this radius is in KM => if miles are needed it is calculated during setter of Place.distance
double nRadius = 6371;
// Get the difference between our two points
// then convert the difference into radians
double nDLat = (firstCoords.latitude - secondCoords.latitude)* (M_PI/180);
double nDLon = (firstCoords.longitude - secondCoords.longitude)* (M_PI/180);
double nLat1 = secondCoords.latitude * (M_PI/180);
double nLat2 = secondCoords.latitude * (M_PI/180);
double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );
double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = nRadius * nC;
NSLog(#"Distance is %f",nD);
return nD; // converts to miles or not (if en_) => implicit in method
}
I have seen the other answers, don't know if they are right, but I think there is a better solution:
(from documentation):
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
You can use it like this:
- (CLLocationDistance) DistanceBetweenCoordinate:(CLLocationCoordinate2D)originCoordinate andCoordinate:(CLLocationCoordinate2D)destinationCoordinate {
CLLocation *originLocation = [[CLLocation alloc] initWithLatitude:originCoordinate.latitude longitude:originCoordinate.longitude];
CLLocation *destinationLocation = [[CLLocation alloc] initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
CLLocationDistance distance = [originLocation distanceFromLocation:destinationLocation];
[originLocation release];
[destinationLocation release];
return distance;
}
Here is code for that..
-(NSString *)findDistanceBetweenTwoLatLon
{
int intEarthRadius = 3963;
double dblLat1 = DegreesToRadians(firstLatitude);
double dblLon1 = DegreesToRadians(firstLongitude);
double dblLat2 = DegreesToRadians(secondLatitude);
double dblLon2 = DegreesToRadians(secondLongitude);
float fltLat = dblLat2 - dblLat1;
float fltLon = dblLon2 - dblLon1;
double a = sin(fltLat/2) * sin(fltLat/2) + cos(dblLat2) * cos(dblLat2) * sin(fltLon/2) * sin(fltLon/2) ;
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = intEarthRadius * c;
double dMeters = d * kOneMileMeters;
NSString *strDistance = [NSString stringWithFormat:#"%1.2f meters",dMeters];
return strDistance;
}
Define all this Macro..
and for degrees to radians
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
where M_PI
#define M_PI 3.14159265358979323846264338327950288
#define kOneMileMeters 1609.344
You can try another approach.You can fetch the lat-long of both the points(assuming lat-long finding will not take that much time) and there are formulae to calculate the distance using lat-long.