6

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 )

enter image description here

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..

Community
  • 1
  • 1
  • Is there any reason you cannot use distanceFromLocation? Because this is the most accurate way to calculate distance and you can make use of this to calculate min and max value. Don't use 111 number, it is only for equator. – brianLikeApple Sep 09 '13 at 11:03
  • @brianLikeApple because i have list of lat and longi in my DB, so it is bad for check each time that region is in my location (500 meter) or not , so.. –  Sep 09 '13 at 11:07
  • No, You don't need to check through all your DB if your DB is really big. Here, I say make use of distanceFromLocation means you can try to guess CLLocation position by adding or minus some value on. distanceFromLocation algorithm is considering everything you need and it provide you better accuracy. – brianLikeApple Sep 09 '13 at 11:15
  • i know it :( i Have table in DB in which 20,000 data and i need to retrive only those data which lati and longi of 500 meter under 500 meter so hows `distanceFromLocation:` is better from me even i require in case compare with all lati and longi from DB :( –  Sep 09 '13 at 11:22
  • I think he needs the inverse of `distanceFromLocation` to be able to determine the perimeter points. Such service is available at Parse.com In the current case using a prefiltering in the DB and then running the `distanceFromLocation` function for the result may be a more robust solution, though. But we don't know enough of the application to say that this is applicable or not. – allprog Sep 09 '13 at 11:26
  • @allprog Yes, you are right. If you check the CLLocation distanceFromLocation description, there is saying "This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth.". So it does the perimeter points stuff. But "The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations." – brianLikeApple Sep 09 '13 at 11:56
  • I have the same problem.Have you find a solution ? – doxsi Jun 27 '14 at 11:54

5 Answers5

1

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.

allprog
  • 16,540
  • 9
  • 56
  • 97
  • No, You cannot do in this way. 1 degree is 111km is only for equator. As one moves away from the equator towards a pole, however, one degree of longitude is multiplied by the cosine of the latitude, decreasing the distance, approaching zero at the pole. – brianLikeApple Sep 09 '13 at 10:58
0

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

Community
  • 1
  • 1
Jakub
  • 13,712
  • 17
  • 82
  • 139
0

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

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
0

I would do this way.

double accuracy = 0.1;//How accurate do you want. Smaller value, slower perform
double distance = 500;//Distance you want
  1. Create infinite loop.
  2. In the loop check whether distance is bigger than 500. If yes, break. If not, add 0.1 value to latitude or longitude.
  3. Do above way to get Max longitude, max latitude, min longitude and min latitude.
  4. 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!

brianLikeApple
  • 4,262
  • 1
  • 27
  • 46
0

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)));
Gollm
  • 120
  • 5