Calculate Latitude and longitude more between Latitude/Longitude points? - iphone

Latitude: 22.744812,
Longitude: 75.892578
The above would be considered my center point.
And now I need to determine the latitude and longitude points from center point 1000 meter outward to each NSWE corners. So I would have a central long/lat, N, S, E and W long/lat..
So I would end up with 4 additional lat/long pairs.
What I am trying to resolve is a formula, preferably that can be done on a standard calculator to determine these 4 NSWE points based on the central point.

You could use MapKit for that:
- (CLLocationCoordinate2D *) calculateSquareCoordinates:(CLLocation*)center withRadius:(float)radius{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center.coordinate, radius*2, radius*2);
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
points[1] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
points[2] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
points[3] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
return points;
}
and just call
CLLocationCoordinate2D *fourPoints = [self calculateSquareCoordinates:center withRadius:1000];
on your code.

you will have to use the Haversine formula to calculate the Lat/Long based on distance from a starting Lat/Long. have a look at this Link

The average radius of the earth is around 6371000 metres. This means that
1 degree of lattitude is equivalent to 6371000 * PI / 180 metres
(NB: PI = 3.14159... etc). However, 1 degree of longitude depends on the lattitude that you are. At the equator, one degree of longitude corresponds to the same distance in metres as 1 degree of lattitude. However, at the north and south poles, all longitude values are the same point (i.e. the pole itself), so 1 degree of longitude at the poles is zero metres. The formula for longitude is
1 degree of longitude is equivalent to 637100 * PI / 180 * COS(Lattitude)
where COS is the trigonometric cosine function. If you make these conversions, then you can do the calculation on a standard calculator. However, be aware that these are approximations that work well over short distances (e.g. less than a few hundred kilometers), but over long distances (e.g. thousands of kilometers) they become more and more inaccurate.

Related

Matlab GPS + distance in meters

I have to calculate for a given Latitude (lat0) and Longitude (lon0) the new latitude (lat1) and longitude (lon1) if I move a distance x[m] and y[m] away from the initial position. For example:
clear all; clc;
%initial coordinates:
lat0=56;
lon0=5;
%moving away from lat0,lon0,
xcor=200; %[m]
ycor=100; %[m]
First I use this code to calculate lat1 and lon1: (source: Adding distance to a GPS coordinate)
lat1=lat0+rad2deg((xcor/6372800))
lon1=lon0+rad2deg((ycor/6372800)/(cos(lat0)))
distance=sqrt(xcor^2+ycor^2)
Now i want to check my answer with the haversine equation:
dlat = deg2rad(lat1-lat0);
dlon = deg2rad(lon1-lon0);
lat0 = deg2rad(lat0);
lat1 = deg2rad(lat1);
a = (sin(dlat./2)).^2 + cos(lat0) .* cos(lat1) .* (sin(dlon./2)).^2;
c = 2 .* asin(sqrt(a));
distance_check=6372800*c
However, most of the time the distance from haversine is different with 100+ meters compared to the calculated distance in the first 3 lines of code.
What is going wrong in this code?
In the first code, 2nd line, cos(lat0) has to be cos(deg2rad(lat0)).

Find Minimum/Maximum latitude and Longitude

My Question is how can i find minimum and maximum latitude and longitude of specific area (500 meter) from current location.
In my case, Such like i need to get X and Y CLLocation (latitude and longitude) from 500meter of area
See my image (sorry for this may be bad drawing )
I also have to tried to googling and i got link such like
How can i get minimum and maximum latitude and longitude using current location and radius?
But i don't know how it implement in my case.
Pleas help me in this issue.
NOTE : I do not want to use CLLocationDistance distance = [currentLocation distanceFromLocation:newLocation]; because it is not helpful in my case so..
If you don't need a really precise value, then use the approximation that 1 degree is 111 km. Based on this, you need to add and remove 0.0025 degrees to the current coordinates to get corners of the area you are looking for.
rectanglesidelengthmeters = 500
degreedeltalat = 0.001 * (rectanglesidelengthmeters / 2.0) * cos(current.lon)
degreedeltalon = 0.001 * (rectanglesidelengthmeters / 2.0) * cos(current.lat)
minlat = current.lat - degreedeltalat
maxlat = current.lat + degreedeltalat
minlon = current.lon - degreedeltalon
maxlon = current.lon + degreedeltalon
You may need to correct the result a little for staying in the -90 .. 90 range for latitude and -180 .. 180 range for longitude values but I think CLClocation will handle that for you too.
You have to do some radius calculation from current location in km.
double kilometers = 0.5;
double curve = ABS( (cos(2 * M_PI * location.coordinate.latitude / 360.0) ));
MKCoordinateSpan span;
span.latitudeDelta = kilometers/111; //like allprog said.
span.longitudeDelta = kilometers/(curve * 111);
MKCoordinateRegion region;
region.span = span;
region.center = location.coordinate;
[self.mapView setRegion:region animated:YES];
This way i set mapView to show distance region to 0.5 km.
EDIT:
Whoa, i digging in my old 'liked' question to show you some original answer, but found a better one below accepted one:
how to make mapview zoom to 5 mile radius of current location
Look at #Anurag answer
To get precise value you should try with
minLattitude = currentLattitude - (RadiusInKm/111.12);
maxLattitude = currentLattitude + (RadiusInKm/111.12);
Thus in your case RadiusInKm = 0.5
For finding in & max longitude data you need to follow the same thing but but you have to multiply the result with cosine function of latitude
I would do this way.
double accuracy = 0.1;//How accurate do you want. Smaller value, slower perform
double distance = 500;//Distance you want
Create infinite loop.
In the loop check whether distance is bigger than 500. If yes, break. If not, add 0.1 value to latitude or longitude.
Do above way to get Max longitude, max latitude, min longitude and min latitude.
Compare your DB, if CLLocation is inside of the value, then return.
I cannot say this is the best way to solve your problem. Because we are guessing value...If you know how to convert CLLocation from given distance, that is better!
This should be correct (in php)
https://www.movable-type.co.uk/scripts/latlong-db.html
$R = 6371; // earth's mean radius, km
$rad = 0.5
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
$maxLon = $lon + rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$minLon = $lon - rad2deg(asin($rad/$R) / cos(deg2rad($lat)));

Latitude / Longitude Distance Calculation

A quick question about a Lat / Long calculation.
I want to take a value set e.g. Lat: 55.123456 Long -6.123456 and work out the four points that are an arbitrary distance away.
As the given square, I want to work out the value for Latitude on the left and right side. Thus the red lines are 1.5km from the start point. Likewise for the longitude, the blue lines will be 1.5km from the start point. The output will be 4 points, all distances in kilometres.
In short: Latitude + Y = Latitude Value X kilometers away
Working with iPhone at the moment and its for a very rough database calculation.
EDIT: Just to clarify, the distance is so short that curvature (And hence accuracy) is not an issue.
In OBJ-C this should be a decent solution:
float r_earth = 6378 * 1000; //Work in meters for everything
float dy = 3000; //A point 3km away
float dx = 3000; //A point 3km away
float new_latitude = latitude + (dy / r_earth) * (180 / M_PI);
float new_longitude = longitude + (dx / r_earth) * (180 / M_PI) / cos(latitude * 180/M_PI);
Well, for rough calculation with relatively small distances (less than 100km) you may assume that there is 40_000_000/360=111 111 meters per degree of latitude and 111 111*cos(latitude) meters per degree of longitude. This is because a meter was defined as 1/40_000_000 part of the Paris meridian;).
Otherwise you should use great circle distances, as noted in the comments. For high precision you also need to take into account that Earth is slightly oblate spheroid rather than a sphere.
// parameter: offset in meters
float offsetM = 1500; // 1.5km
// degrees / earth circumfence
float degreesPerMeter = 360.0 / 40 000 000;
float toRad = 180 / M_PI;
float latOffsetMeters = offsetM * degreesPerMeter;
float lonOffsetMeters = offsetM * degreesPerMeter * cos (centerLatitude * toRad);
Now simply add +/- latOffsetMeters and +/- lonOffsetMeters to your centerLatitude/ centerLongitude.
Formula is usefull up to hundred kilometers.

MKCoordinateSpan in Meters?

I need to create a MKCoordinateSpan that is about 500 meters.
How do I calculate the values to pass into the MKCoordinateSpan constructor?
Answers in any programming (Obj-C, .Net) language are fine.
Another alternative is to use MapKit's MKCoordinateRegionMakeWithDistance function:
MKCoordinateRegion rgn = MKCoordinateRegionMakeWithDistance(
CLLocationCoordinate2DMake(someLatitude, someLongitude), 500, 500);
The MKCoordinateSpan will be in rgn.span.
Unless you need great accuracy you can make it much easier with approximation. The first problem is to find the fraction of a degree of latitude representing 500 meters. Easy since a degree of latitude is a constant in any location, roughly 111 km. So 500 meters is .0045 degrees latitude.
Then it gets harder because length of a degree of longitude varies depending on where you are. It can be approximated with
where alpha is earth's equatorial radius, 6,378,137km, b/a is 0.99664719 (a constant in use for the WGC84 spheroid model in use by all GPS devices) and where phi is the degree of latitude.
Imagine for a second you're lucky enough to be in Melbourne with a longitude of 37.783 degrees S. North or South doesn't matter here. beta works out to be 37.6899 and the rest of it solves to give a longitudinal degree a length of 88km. So 500 meters is .0057 of a degree.
Result for Melbourne - MKCoordinateSpan melbourne500MeterSpan = MKCoordinateSpanMake(.0045, .0057);
You can check your answers and your code with this online calculator
The wiki article on longitude has a lot more detail on this (and it the source of the images here)
Code:
#define EARTH_EQUATORIAL_RADIUS (6378137.0)
#define WGS84_CONSTANT (0.99664719)
#define degreesToRadians(x) (M_PI * (x) / 180.0)
// accepts decimal degrees. Convert from HMS first if that's what you have
double spanOfMetersAtDegreeLongitude(double degrees, double meters) {
double tanDegrees = tanf(degreesToRadians(degrees));
double beta = tanDegrees * WGS84_CONSTANT;
double lengthOfDegree = cos(atan(beta)) * EARTH_EQUATORIAL_RADIUS * M_PI / 180.0;
double measuresInDegreeLength = lengthOfDegree / meters;
return 1.0 / measuresInDegreeLength;
}
In MonoTouch, then using this solution you can use this helper method:
public static void ZoomToCoordinateAndCenter (MKMapView mapView, CLLocationCoordinate2D coordinate, double meters, bool showUserLocationToo, bool animate)
{
if (!coordinate.IsValid ())
return;
mapView.SetCenterCoordinate (coordinate, animate);
mapView.SetRegion (MKCoordinateRegion.FromDistance (coordinate, meters, meters), animate);
}

How do I calculate the distance between two points of latitude and longitude? [duplicate]

This question already has answers here:
Distance between two coordinates with CoreLocation
(4 answers)
Closed 8 years ago.
i have latitude and longitude of particular place and i want to calculate the distance so how can i calculate it?
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(#"Distance i meters: %f", [location1 distanceFromLocation:location2]);
[location1 release];
[location2 release];
You also need to add CoreLocation.framework to your project, and add the import statement:
#import <CoreLocation/CoreLocation.h>
This might not be the most efficient method of doing it, but it will work.
Your two locations specified by latitude and longitude can be considered vectors. Assuming that the coordinates have been converted into cartesion coordinates, calculate the dot product of the two vectors.
Given v1 = (x1, y1, z1) and v2 = (x2, y2, z2), then ...
v1 dot v2 = magnitude(v1) * magnitude(v2) * cos (theta)
Conveniently, the magnitude of v1 and v2 will be the same ... the radius of the earth (R).
x1*x2 + y1*y2 + z1*z2 = R*R*cos(theta)
Solve for theta.
theta = acos ((x1*x2 + y1*y2 + z1*z2) / (R * R));
Now you have angle between the two vectors in radians. The distance betwen the two points when travelling across the surface of earth is thus ...
distance = theta * R.
There is probably an easier way to do this entirely within the context of spherical coordinates, but my math in that area is too fuzzy--hence the conversion to cartesian coordinates.
To convert to cartesian coordinates ...
Let alpha be the latitude, and beta be the longitude.
x = R * cos (alpha) * cos (beta)
y = R * sin (alpha)
z = R * cos (alpha) * sin (beta)
Don't forget that the math function typically deal in radians, and the latitude/longitude deal in degrees.
I've cranked through the math, and can now greatly simplify the solution.
Imagine if we spin the earth so that our first vector is at 0 degrees latitude and 0 degrees longitude. The second vector would be at (alpha2 - alpha1) degrees latitude and (beta2 - beta1) degrees latitude.
Since ...
sin(0) = 0 and cos(0) = 1
our dot product simplies to ...
cos(delta_alpha) * cos(delta_beta) = cos(theta)
The rest of the math remains unchanged.
theta = acos (cos(delta_alpha) * cos(delta_beta))
distance = radius * theta
Hope this helps.