user2289379
user2289379

Reputation:

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 )

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

Upvotes: 6

Views: 6775

Answers (5)

Gollm
Gollm

Reputation: 130

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)));

Upvotes: 0

brianLikeApple
brianLikeApple

Reputation: 4371

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!

Upvotes: 0

Niru Mukund Shah
Niru Mukund Shah

Reputation: 4733

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

Upvotes: 0

allprog
allprog

Reputation: 16790

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.

Upvotes: 1

Jakub
Jakub

Reputation: 13860

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

Upvotes: 0

Related Questions