Calculating a location coordinate a certain distance from another coordinate - iphone

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.

Related

How to accurately store geolocation data, filter within a radius, and calculate distance?

I write new locations with:
ST_SetSrid(ST_MakePoint(:longitude, :latitude), 4326)
They get saved under "profile".location:
SRID=4326;POINT(-75.1234 35.1234)
I try to filter profiles within some :radius in meters where :ownLocation is some location string that consists of a bunch of numbers:
ST_DWithin("profile".location, :ownLocation::geometry, :radius)
Then when I get the results back and calculate their distance with:
ST_Distance(
ST_Transform("profile".location::geometry, 3857::int), ST_Transform(:ownLocation::geometry, 3857::int)
) as distance
and convert the distance from meters to miles, my results are a bit off. Let's say I set a max radius of within 10 miles - the distance I get back seems anywhere from 0-23 miles (would expect 0-10 miles).
I am wondering where I am going wrong here, and have a feeling it may have to do with projections, or I am using the functions incorrectly.
Update after solution: store as 4326, display distance as 3857
store as 4326:
ST_SetSrid(ST_MakePoint(:longitude, :latitude), 4326)
filter using geography type so it can accept filter radius as meters that users will pass in:
ST_DWithin("profile".location::geography, :ownLocation::geography, :radiusInMeters)
display distance from 4326 to 3857 meters with correction (https://postgis.net/docs/ST_Distance.html):
ST_Distance(
ST_Transform("profile".location::geometry, 3857),
ST_Transform(:ownLocation::geometry, 3857)
) * cosd(42.3521) as distance
3857 is not suitable for computing distances, as it introduces important distortions as you move away from the equator.
Instead, you can use ST_Distance using the geography datatype:
ST_Distance("profile".location::geography,:ownLocation::geography) as distance
Regarding st_dwithin, it uses the projection unit, which is degrees, not meters. You can also use the geography datatype here.
The unit of EPSG 4326 is degrees whereas the unit of EPSG 3857 is meters.
ST_DWithin("profile".location, :ownLocation::geometry, :radius) will take degrees as radius not meters or miles.
Transform "profile".location and :ownLocation::geometry to 3857 should work;

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)

Finding distance between two points using latitude and longitude in Degree Minutes Seconds format

I am working on a tool to find the distance between two points whose latitude and longitude are given. Its ok when the latitude and longitude is given in Signed Degree Format. But I couldn't find a way to calculate the distance when latitude and longitude is given in Degree Minutes Seconds Format (ex: N 11° 14' 52').Can anybody suggest me a way to find the distance ?
So first convert the DMS coordinates to your degree format.
e.g. 11°14'52" = 11 + 14/60 + 52/3600 = 11.24777778 degrees
and then continue as you did with your destance calculation that you say already works.
Edit:
Note that latitudes in the southern hemisphere and longitudes in the western hemisphere are negative. So the above formula really should be:
sign(degrees) * (abs(degrees) + minutes/60 + seconds/3600)

Convert Latitude / Longitude in Degree/Radians?

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.