Convert Latitude / Longitude in Degree/Radians? - iphone

How can i convert Latitude and longitude in degree/radians?
Do you guys any formula or any idea?
I want to show it on MapKit.
Thanks.....

Since Latitude and Longitude are measured in degrees, you can use the following formula to convert to radians, and back to degrees:
Radians = Degrees * PI / 180
and on the inverse,
Degrees = Radians * 180 / PI

If you look at earth like a sphere, latitude and longitude are already given in units of degrees. Longitude is expressed as -180 degrees (-pi radians) to 180 degrees (pi radians) with 0 degrees centered at the prime meridian. Latitude is expressed as -90 degrees (-pi/2 radians) to 90 degrees (pi/2 radians) with respect to the equator. This is already a spherical coordinate system (depending on the caveats I give below) with earth's radius approximately 6371 km.
So just convert by multiplying by pi/180 as you would for convert degrees to radians in any normal sense. If you want to use those radians for doing something specific like calculating distances between two lat/lons then you should look at some pre-existing sources.
Similar questions have been asked before, i.e. Convert Lat-Lon to Cartesian Coordinates.
Generally speaking, the 'correct' answer for a lot of conversion quesitons depends on what error is acceptable in your answer (can it be off 1 mile for distances of 20 miles, etc.) and the model of the world is appropriate in your problem domain. The world is better approximated by an ellipsoid (squashed sphere) than a sphere, but it is easier to perform the calculations assuming earth is a sphere and the error introduced can be minimal. Especially if your program is just looking for 'rough' answers that won't be used for, say, engineering projects.
It is likely that the lat-lons you have are given in a coordinate system called WGS-84 which is what most hardware GPS units use, which assumes an ellipsoid model.
Note that I had a lot more sources, but I guess I have low reputation so I can only post two links. I would suggest reading on wikipedia about WGS-84, earth's radius, spherical coordinates, prime meridian, and equator for some visual references.

Related

Question about Matlab's aerospace toolbox's gravitysphericalharmonic library

I'm currently trying to learn how to use the gravity models that Matlab's 'gravitysphericalharmonics' library has. In this documentation: https://www.mathworks.com/help/aerotbx/ug/gravitysphericalharmonic.html#mw_3bdd1e99-46be-4fd5-8b7c-6dd710546616
It gives us two examples:
Calculate the gravity in the x-axis at the equator on the surface of Earth. This example uses the default 120 degree model of EGM2008 with default warning actions.
gx = gravitysphericalharmonic([-6378.137e3 0 0])
Calculate the gravity at 25,000 m over the south pole of Earth.
[gx, gy, gz] = gravitysphericalharmonic([0 0 -6381.751e3],'EGM96','Error')
Here's my question, how did they get -6381.751e3 for the second question if they're just finding the gravity 25,000m above the surface of Earth? If Earth's radius is ~6378.137e3 meters, then calculating the gravity at 25,000m above Earth would just be the radius + 25,000 meters right?
Lastly, I wanted to just ask someone with more knowledge about this module why it was -6378.137e3 m for the first question. If we're taking the coordinate from just the surface of Earth, wouldn't that number have to be positive?
Sorry if this doesn't make any sense, I'm new to this stuff and I really want to learn! Thanks!
The first example uses a 3D position vector, in a coordinate system with its origin at the center of the planet (Earth). The position vector[-6378.137e3 0 0] is on the equator, as is [6378.137e3 0 0] (they are on opposite sides of the world). Any other position vector with magnitude 6378.137e3 and a Z component (the third number) of 0 would also be on the equator.
The Earth is not a perfect sphere; it is an oblate spheroid. The radius at the equator is 6378.137 km, but the radius at the poles is 6356.752 km. Their documentation is not great here, but they have arrived at the position vector in the second example by taking this polar radius and increasing the magnitude of the vector by 15 km. The Z component is negative because this example is at the South Pole; if the Z component were positive, it would be at the North Pole. Because it is negative, they have to add -15 km to get to a point that is 15 km above the surface (but if you were looking at a globe, and holding the globe so that north is up, that point would be below the globe).
They have probably used examples with negative values in the position vector precisely to ensure that people ask themselves the kinds of questions that you are asking.
The aerospace toolbox also provides a function ecef2lla to convert these position vectors into latitude, longitude, and altitude, which may help you understand them.

Calculating longitude distortion on approach to poles?

I'm working on a game in which players are trying to target points on a sphere.
As targets approach the poles, the game should become more forgiving in longitude because the ratio of longitude distance to latitude distance grows. At the poles, the game should be infinitely forgiving in longitude.
What formula am I looking for that tells me how much longitudinal distance equals 1 unit of latitudinal distance for a particular latitude? Assume that longitude and latitude are expressed as floats ranging from 0-1, and at the equator (0.5) longitude equals latitude.
Formula is rather simple (for usual spherical coordinates with -Pi/2..Pi/2 latitude range):
Lon_Lat_Ratio = Cos(Latitude)
But your parametrization is different, so
Lon_Lat_Ratio = Sin(Latitude_01 * Pi)
Ratio is 1 on equator and 0 at poles

Calculate longitude on the 0 degree latitude line, knowing the starting point (latitude,longitude) and azimuth on ellipsoid Earth model

Input:
Observer (Lat,Lon WGS84 coordinates) and an azimuth angle (degrees)
Output:
By considering only the 0 degrees latitude line, I am trying to get the intersection longitude point from the observer point with the given azimuth. I am considering the ellipsoid Earth model.
If you have the Mapping Toolbox available, you can use the track1 function. See the Mathworks documentation at http://www.mathworks.com/help/map/ref/track1.html?refresh=true.
Basic formula is
lon(lat, lon, Z) = lon - atan(sin(lat)*tan(Z))
(Beware that your calculator uses degrees)

Calculating a location coordinate a certain distance from another coordinate

I have a CLLoctionCoordinate2D and I want to figure out what the points are a given distance away from it. For an example, lets use 100 miles. So I want to computer the point 100 miles to the north, south, east, and west of it.
East and west are pretty easy. I converted both coordinates to MKMapPoints and used distanceInMeters*MKMapPointsPerMeterAtLatitude(latitude). This works fine for east and west because the latitude is constant.
However, this does not work for north and south. To the north, it underestimates and to the south it underestimates (because MKMapPointsPerMeterAtLatitude is changing as you move). How could I compute these points for the north and south?
There's 60 nautical miles per degree of latitude. This is also true for longitude, but at the equator only. To calculate the distance in longitude in nautical miles, multiply the distance in degrees longitude * 60nm * cos( latitude in radians ).
Do the calculations of your new points (100 miles north, 100 miles south, etc.) in lat lon, not MKMapPoints, then convert your lat/lon (CLLocationCoordinate2D ?) points to MKMapPoints.
Lines of latitude are always a constant angle apart, as measured from the earth's core that's kinda their point. So what you need to do is work out how far apart in terms of miles they are, an then use that to get 100 miles north/south and then get the map points for that. The formula for the distance at the earth's surface will be online somewhere, probably in metres or kilometres.

Equation for distance between two points on the map using latitude and longitude iphone

I am trying to calculate the distance between two points (Pins) on the iPhone map.
Everything works fine however if you put one of the pin close to the left edge and the other one to the right edge of the map, distanceFromLocation method always returned the shortest distance between points (of course earth is not flat). I tried switching the CLLocations but it still shows me the shortest distance.
For my app I will need to calculate both, shortest and the longest distance between two pins.
The problem seems to be trivial but I can't come out with anything to solve it.
Any help or clues appreciated. Thanks!
There is a function MKCoordinateRegionForMapRect that returns a MKCoordinateRegion. Without having tried it, seems like all you would need to do is accurately specify the rect as the region bounded at one diagonal by one pin and the other diagonal by the other pin. You could use CGRectUnion twice giving the pin origins as rect origin and an arbitrarily small width and height.
Then the distance between the pins will be the square root of the distances represented by the latitudeDelta and longitudeDelta returned in the span squared and added. (i.e. Pythagoras theorem, the two side lengths are the horizontal and vertical sides of a triangle and the diagonal is a straight line between your points.)
Finding latitudeDelta in metres is easy, each degree of latitude is 111km.
To find meters for the longitudeDelta you need a little bit more 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 lengthOfDegreeLongitude(double degrees) {
double tanDegrees = tanf(degreesToRadians(degrees));
double beta = tanDegrees * WGS84_CONSTANT;
double lengthOfDegree = cos(atan(beta)) * EARTH_EQUATORIAL_RADIUS * M_PI / 180.0;
return lengthOfDegree;
}
Give that function the longitude of the location you are interested in and multiply the result by the longitudeDelta in degrees to get a distance in meters. Then you have a horizontal distance and a vertical and you can find the diagonal which is what you were looking for.
Inaccuracy will get bad if you are looking for a distance which is very large in N-S as the length of a degree longitude will be varying over the calculation. And you can refine the 111km for a degree of latitude a bit too, that's just from memory.