i am making an navigation based application. In this application i am drawing a route from points selected by the user. I have requirement of recalculating route if user is not following the route.
for Calculating the route i have used Google direction API. and for drawing the route i have used this code
- (void) drawRoute:(NSArray *) path
{
NSInteger numberOfSteps = path.count;
[self.objMapView removeOverlays: self.objMapView.overlays];
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++)
{
CLLocation *location = [path objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
for( id <MKOverlay> ovr in [self.objMapView overlays])
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:ovr];
if (polylineView.tag == 22)
{
[self.objMapView removeOverlay:ovr];
}
[polylineView release];
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.objMapView addOverlay:polyLine];
}
Till now every thing is okey.
Now, i want a notification if user is out of route (more than 100 meters).and i can get the notification also
PROBLEM:~ if road is straight (more than 100mt) then i cant get points on the road. To explain the problem i have attached the image...
In this image suppose black line is my path (polyline) and red circles are the points i got form google apis. but in the straight path shown as blue circle i cant get points to compare and in this path recalculation function is called.
Can any one tell me the solution from which i can get all points of route even if it is a straight road.
I know this is an old thread, but recently ran into the same problem and found an OK solution.
The concept is that you don't calculate the distance to EACH line segment but only to the TWO segments connected to the closest point.
calculate the distance of your current location to all the points in the
MKPolyline and take the minimum from that. (There's probably some nice way to optimize this. Like not iterating through all the points on every location update, but don't have time to dig in to that now).
You now know the distance to the closest polyline-point. However that point might still be far away while the polyline itself (connecting this point and the previous or the next point) might be closer. So, calculate the distance between your current location and these two line-segments and you have the closest distance.
Now, this is not waterproof. While it minimizes the nr of api calls, on some occasions (If you have crazy bends and curves in the MKPolyline) it might call the api while not needed, but hey, then the same line will be drawn again, no damage done. In my tests it worked fine and you can also adjust the accuracy. I've set it to 200m (0.2km) in the code below.
//Get Coordinates of points in MKPolyline
NSUInteger pointCount = routeLineGuidanceTurn.pointCount;
CLLocationCoordinate2D *routeCoordinates = malloc(pointCount * sizeof(CLLocationCoordinate2D));
[routeLineGuidanceTurn getCoordinates:routeCoordinates
range:NSMakeRange(0, pointCount)];
NSLog(#"route pointCount = %d", pointCount);
//Determine Minimum Distance and GuidancePoints from
double MinDistanceFromGuidanceInKM = 1000;
CLLocationCoordinate2D prevPoint;
CLLocationCoordinate2D pointWithMinDistance;
CLLocationCoordinate2D nextPoint;
for (int c=0; c < pointCount; c++)
{
double newDistanceInKM = [self distanceBetweentwoPoints:Currentcordinate.latitude longitude:Currentcordinate.longitude Old:routeCoordinates[c].latitude longitude:routeCoordinates[c].longitude];
if (newDistanceInKM < MinDistanceFromGuidanceInKM) {
MinDistanceFromGuidanceInKM = newDistanceInKM;
prevPoint = routeCoordinates[MAX(c-1,0)];
pointWithMinDistance = routeCoordinates[c];
nextPoint = routeCoordinates[MIN(c+1,pointCount-1)];
}
}
free(routeCoordinates);
NSLog(#"MinDistanceBefore: %f",MinDistanceFromGuidanceInKM);
//If minimum distance > 200m we might have to recalc GuidanceLine.
//To be sure we take the two linesegments connected to the point with the shortest distance and calculate the distance from our current position to that linedistance.
if (MinDistanceFromGuidanceInKM > 0.2) {
MinDistanceFromGuidanceInKM = MIN(MIN([self lineSegmentDistanceFromOrigin:Currentcordinate onLineSegmentPointA:prevPoint pointB:pointWithMinDistance], [self lineSegmentDistanceFromOrigin:Currentcordinate onLineSegmentPointA:pointWithMinDistance pointB:nextPoint]),MinDistanceFromGuidanceInKM);
if (MinDistanceFromGuidanceInKM > 0.2) {
// Call the API and redraw the polyline.
}
}
Here's the fun that calculate sthe distance between two points. I know there is a built in function for it, but had it in my code already.
-(double)distanceBetweentwoPoints:(double)Nlat longitude:(double)Nlon Old:(double)Olat longitude:(double)Olon {
//NSLog(#"distanceBetweentwoPoints");
double Math=3.14159265;
double radlat1 = Math* Nlat/180;
double radlat2 = Math * Olat/180;
double theta = Nlon-Olon;
double radtheta = Math * theta/180;
double dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * cos(radlat2) * cos(radtheta);
if (dist>1) {dist=1;} else if (dist<-1) {dist=-1;}
dist = acos(dist);
dist = dist * 180/Math;
dist = dist * 60 * 1.1515;
return dist * 1.609344;
}
And here's the bit that calculates the distance between a point and a line segment between two other points. I got this from here: https://stackoverflow.com/a/28028023/3139134 Modified it a bit to work with CLLocationCoordinate2D and return the distance.
- (CGFloat)lineSegmentDistanceFromOrigin:(CLLocationCoordinate2D)origin onLineSegmentPointA:(CLLocationCoordinate2D)pointA pointB:(CLLocationCoordinate2D)pointB {
CGPoint dAP = CGPointMake(origin.longitude - pointA.longitude, origin.latitude - pointA.latitude);
CGPoint dAB = CGPointMake(pointB.longitude - pointA.longitude, pointB.latitude - pointA.latitude);
CGFloat dot = dAP.x * dAB.x + dAP.y * dAB.y;
CGFloat squareLength = dAB.x * dAB.x + dAB.y * dAB.y;
CGFloat param = dot / squareLength;
CGPoint nearestPoint;
if (param < 0 || (pointA.longitude == pointB.longitude && pointA.latitude == pointB.latitude)) {
nearestPoint.x = pointA.longitude;
nearestPoint.y = pointA.latitude;
} else if (param > 1) {
nearestPoint.x = pointB.longitude;
nearestPoint.y = pointB.latitude;
} else {
nearestPoint.x = pointA.longitude + param * dAB.x;
nearestPoint.y = pointA.latitude + param * dAB.y;
}
CGFloat dx = origin.longitude - nearestPoint.x;
CGFloat dy = origin.latitude - nearestPoint.y;
return sqrtf(dx * dx + dy * dy) * 100;
}
For each pair of points in each step, you can calculate the distance between them using the Pythagorean Theorem:
distance = sqrt( pow((point1.x - point2.x), 2) + pow((point1.y - point2.y), 2) )
Then, if the distance is greater than 100m, add intermediary points along the line segment.
Related
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Making use of velocityInView with UIPanGestureRecognizer
I am creating an app in which I am drawing a path based on touchMoved. I want to make the width of line based on the velocity of touch. I can achieve this effect, but there is no smoothness in this.
CODE:
-(float) clampf:(float) v: (float) min: (float) max
{
if( v < min ) v = min;
if( v > max ) v = max;
return v;
}
- (float)extractSize:(UIPanGestureRecognizer *)panGestureRecognizer
{
vel = [gesture velocityInView:self.view];
mag = sqrtf((vel.x * vel.x) + (vel.y * vel.y)); // pythagoras theorem (a(square) + b(square) = c(square))
final = mag/166.0f;
final = [self clampf:final :1 :40];
NSLog(#"%f", final);
if ([velocities count] > 1) {
final = final * 0.2f + [[velocities objectAtIndex:[velocities count] - 1] floatValue] * 0.8f;
}
[velocities addObject:[NSNumber numberWithFloat:final]];
return final;
}
But i am getting something like this:
I have made the similar effect before. I was using the original touch event methods (touchesBegan, etc.) and calculated the velocity by myself. Every thing looks all right.
I am not sure if the velocityInView method always gives your the correct result during user moving their finger, maybe you should try to calculate the velocity by yourself and see if the problem is gone.
I must be missing somthing out in the docs, I thought this should be easy...
If I have one coordinate and want to get a new coordinate x meters away, in some direction. How do I do this?
I am looking for something like
-(CLLocationCoordinate2D) translateCoordinate:(CLLocationCoordinate2D)coordinate
translateMeters:(int)meters
translateDegrees:(double)degrees;
Thanks!
Unfortunately, there's no such function provided in the API, so you'll have to write your own.
This site gives several calculations involving latitude/longitude and sample JavaScript code. Specifically, the section titled "Destination point given distance and bearing from start point" shows how to calculate what you're asking.
The JavaScript code is at the bottom of that page and here's one possible way to convert it to Objective-C:
- (double)radiansFromDegrees:(double)degrees
{
return degrees * (M_PI/180.0);
}
- (double)degreesFromRadians:(double)radians
{
return radians * (180.0/M_PI);
}
- (CLLocationCoordinate2D)coordinateFromCoord:
(CLLocationCoordinate2D)fromCoord
atDistanceKm:(double)distanceKm
atBearingDegrees:(double)bearingDegrees
{
double distanceRadians = distanceKm / 6371.0;
//6,371 = Earth's radius in km
double bearingRadians = [self radiansFromDegrees:bearingDegrees];
double fromLatRadians = [self radiansFromDegrees:fromCoord.latitude];
double fromLonRadians = [self radiansFromDegrees:fromCoord.longitude];
double toLatRadians = asin( sin(fromLatRadians) * cos(distanceRadians)
+ cos(fromLatRadians) * sin(distanceRadians) * cos(bearingRadians) );
double toLonRadians = fromLonRadians + atan2(sin(bearingRadians)
* sin(distanceRadians) * cos(fromLatRadians), cos(distanceRadians)
- sin(fromLatRadians) * sin(toLatRadians));
// adjust toLonRadians to be in the range -180 to +180...
toLonRadians = fmod((toLonRadians + 3*M_PI), (2*M_PI)) - M_PI;
CLLocationCoordinate2D result;
result.latitude = [self degreesFromRadians:toLatRadians];
result.longitude = [self degreesFromRadians:toLonRadians];
return result;
}
In the JS code, it contains this link which shows a more accurate calculation for distances greater than 1/4 of the Earth's circumference.
Also note the above code accepts distance in km so be sure to divide meters by 1000.0 before passing.
I found one way of doing it, had to dig to find the correct structs and functions. I ended up not using degrees but meters for lat and long instead.
Here's how I did it:
-(CLLocationCoordinate2D)translateCoord:(CLLocationCoordinate2D)coord MetersLat:(double)metersLat MetersLong:(double)metersLong{
CLLocationCoordinate2D tempCoord;
MKCoordinateRegion tempRegion = MKCoordinateRegionMakeWithDistance(coord, metersLat, metersLong);
MKCoordinateSpan tempSpan = tempRegion.span;
tempCoord.latitude = coord.latitude + tempSpan.latitudeDelta;
tempCoord.longitude = coord.longitude + tempSpan.longitudeDelta;
return tempCoord;
}
And of course, if I really need to use degrees in the future, it's pretty easy (I think...) to do some changes to above to get it to work like I actually asked for.
Using an MKCoordinateRegion has some issues—the returned region can be adjusted to fit since the two deltas may not exactly map to the projection at that latitude, if you want zero delta for one of the axes you are out of luck, etc.
This function uses MKMapPoint to perform coordinate translations which allows you to move points around in the map projection's coordinate space and then extract a coordinate from that.
CLLocationCoordinate2D MKCoordinateOffsetFromCoordinate(CLLocationCoordinate2D coordinate, CLLocationDistance offsetLatMeters, CLLocationDistance offsetLongMeters) {
MKMapPoint offsetPoint = MKMapPointForCoordinate(coordinate);
CLLocationDistance metersPerPoint = MKMetersPerMapPointAtLatitude(coordinate.latitude);
double latPoints = offsetLatMeters / metersPerPoint;
offsetPoint.y += latPoints;
double longPoints = offsetLongMeters / metersPerPoint;
offsetPoint.x += longPoints;
CLLocationCoordinate2D offsetCoordinate = MKCoordinateForMapPoint(offsetPoint);
return offsetCoordinate;
}
Nicsoft's answer is fantastic and exactly what I needed. I've created a Swift 3-y version which is a little more concise and can be called directly on a CLLocationCoordinate2D instance:
public extension CLLocationCoordinate2D {
public func transform(using latitudinalMeters: CLLocationDistance, longitudinalMeters: CLLocationDistance) -> CLLocationCoordinate2D {
let region = MKCoordinateRegionMakeWithDistance(self, latitudinalMeters, longitudinalMeters)
return CLLocationCoordinate2D(latitude: latitude + region.span.latitudeDelta, longitude: longitude + region.span.longitudeDelta)
}
}
I'm trying to develop an application that use the GPS and Compass of the iPhone in order to point some sort of pointer to a specific location (like the compass always point to the North). The location is fixed and I always need the pointer to point to that specific location no matter where the user is located. I have the Lat/Long coordinates of this location but not sure how can I point to that location using the Compass and the GPS... just like http://www.youtube.com/watch?v=iC0Xn8hY80w this link 1:20'
I write some code, however, it can't rotate right direction.
-(float) angleToRadians:(double) a {
return ((a/180)*M_PI);
}
-(void)updateArrow {
double alon=[longi doubleValue];//source
double alat=[lati doubleValue];//source
double blon=[pointlongi doubleValue];//destination
double blat=[pointlati doubleValue];//destination
float fLat = [self angleToRadians:alat];
float fLng = [self angleToRadians:alon];
float tLat = [self angleToRadians:blat];
float tLng = [self angleToRadians:blon];
float temp = atan2(sin(tLng-fLng)*cos(tLat),
cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
double temp2= previousHeading;
double temp1=temp-[self angleToRadians:temp2];
/*I using this,but it can't rotate by :point even i change the coordinate
in CGPointMake */
Compass2.layer.anchorPoint=CGPointMake(0, 0.5);
[Compass2 setTransform:CGAffineTransformMakeRotation(temp1)];
/* Compass2 is a UIImageView like below picture I want to rotate it around
: point in image
^
|
|
|
:
|
*/
There is a standard "heading" or "bearing" equation that you can use - if you are at lat1,lon1, and the point you are interested in is at lat2,lon2, then the equation is:
heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))
This gives you a bearing in radians, which you can convert to degrees by multiplying by 180/π. The value is then between -180 and 180 degrees, so to get a standard compass bearing add 360 to any negative answers.
atan2 is a standard function related to arctan, that does the right thing for the four possible quadrants that your destination point could be in compared to where you are.
1) Get your current location (from the GPS)
2) Get the differences in latitude and longitude
3) use the atan2 method to get the angle
i.e. (WARNING: untested code)
CLLocation *targetLocation = [CLLocation alloc] initWithLatitude:1 longitude:2];
CLLocation *sourceLocation = <get from GPS>
double dx = [targetLocation coordinate].latitude - [sourceLocation coordinate].latitude;
double dy = [targetLocation coordinate].longitude - [sourceLocation coordinate].longitude;
double angle = atan2(dx, dy);
You might have to tweak that to get it to compile but the idea is there!
I did this some time ago, here are two different implementations. The first is similar to your approach, the second is without the trig math. The first is what I used in my app, but the second seemed to work as well, though doesn't appear to be as clean. You will need to also remember to offset this bearing based on north in your UI.
- (double) toRadian: (double) val
{
return val * (M_PI / 180);
}
// Convert to degrees from radians
- (double) toDegrees: (double) val
{
return val * 180 / M_PI;
}
// convert from a radian to a 360 degree format.
- (double) toBearing: (double) val
{
return ( (int)([self toDegrees: val]) + 360 ) % 360; // use mod to get the degrees
}
// Calculate the bearing based off of the passed coordinates and destination.
//
- (double) calcBearingWithLatitude:(CLLocationDegrees)latSource
latitude:(CLLocationDegrees)latDest
longitude:(CLLocationDegrees)lonSrc
longitude:(CLLocationDegrees)lonDest
{
double lat1 = [self toRadian:latSource];
double lat2 = [self toRadian:latDest];
double dLon = [self toRadian:(lonDest - lonSrc)];
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
return [self toBearing:atan2(y, x)];
}
And the second.
// got this code from some forums and modified it, thanks for posting it coullis! Mostly here for reference on how to do this without sin and cos.
- (CLLocationDegrees) altCalcBearingWithLatitude:(CLLocationDegrees)latSource
latitude:(CLLocationDegrees)latDest
longitude:(CLLocationDegrees)lonSrc
longitude:(CLLocationDegrees)lonDest
{
CLLocationDegrees result;
// First You calculate Delta distances.
float dx = lonSrc - latSource;
float dy = lonDest - latDest;
// If x part is 0 we could get into division by zero problems, but in that case result can only be 90 or 270:
if (dx==0)
{
if (dy > 0)
result = 90;
else
result = 270;
}
else
{
result = [self toDegrees: atan(dy/dx)];
}
// This is only valid for two quadrants (for right side of the coordinate system) so modify result if necessary...
if (dx < 0)
result = result + 180;
// looks better if all numbers are positive (0 to 360 range)
if (result < 0)
result = result + 360;
// return our result.
return result;
}
Use this. You will have to subtract out your actual compass heading from the result of getHeadingForDirection to determine the proper relative heading. Return value is heading in radians.
-(float) angleToRadians:(float) a {
return ((a/180)*M_PI);
}
- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = [self angleToRadians:fromLoc.latitude];
float fLng = [self angleToRadians:fromLoc.longitude];
float tLat = [self angleToRadians:toLoc.latitude];
float tLng = [self angleToRadians:toLoc.longitude];
return atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
}
I'm trying to develop an application that use the GPS and Compass of the iPhone in order to point some sort of pointer to a specific location (like the compass always point to the North). The location is fixed and I always need the pointer to point to that specific location no matter where the user is located. I have the Lat/Long coordinates of this location but not sure how can I point to that location using the Compass and the GPS... just like http://www.youtube.com/watch?v=iC0Xn8hY80w this link 1:20'
I write some code, however, it can't rotate right direction.
-(float) angleToRadians:(double) a {
return ((a/180)*M_PI);
}
-(void)updateArrow {
double alon=[longi doubleValue];//source
double alat=[lati doubleValue];//source
double blon=[pointlongi doubleValue];//destination
double blat=[pointlati doubleValue];//destination
float fLat = [self angleToRadians:alat];
float fLng = [self angleToRadians:alon];
float tLat = [self angleToRadians:blat];
float tLng = [self angleToRadians:blon];
float temp = atan2(sin(tLng-fLng)*cos(tLat),
cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
double temp2= previousHeading;
double temp1=temp-[self angleToRadians:temp2];
/*I using this,but it can't rotate by :point even i change the coordinate
in CGPointMake */
Compass2.layer.anchorPoint=CGPointMake(0, 0.5);
[Compass2 setTransform:CGAffineTransformMakeRotation(temp1)];
/* Compass2 is a UIImageView like below picture I want to rotate it around
: point in image
^
|
|
|
:
|
*/
There is a standard "heading" or "bearing" equation that you can use - if you are at lat1,lon1, and the point you are interested in is at lat2,lon2, then the equation is:
heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))
This gives you a bearing in radians, which you can convert to degrees by multiplying by 180/π. The value is then between -180 and 180 degrees, so to get a standard compass bearing add 360 to any negative answers.
atan2 is a standard function related to arctan, that does the right thing for the four possible quadrants that your destination point could be in compared to where you are.
1) Get your current location (from the GPS)
2) Get the differences in latitude and longitude
3) use the atan2 method to get the angle
i.e. (WARNING: untested code)
CLLocation *targetLocation = [CLLocation alloc] initWithLatitude:1 longitude:2];
CLLocation *sourceLocation = <get from GPS>
double dx = [targetLocation coordinate].latitude - [sourceLocation coordinate].latitude;
double dy = [targetLocation coordinate].longitude - [sourceLocation coordinate].longitude;
double angle = atan2(dx, dy);
You might have to tweak that to get it to compile but the idea is there!
I did this some time ago, here are two different implementations. The first is similar to your approach, the second is without the trig math. The first is what I used in my app, but the second seemed to work as well, though doesn't appear to be as clean. You will need to also remember to offset this bearing based on north in your UI.
- (double) toRadian: (double) val
{
return val * (M_PI / 180);
}
// Convert to degrees from radians
- (double) toDegrees: (double) val
{
return val * 180 / M_PI;
}
// convert from a radian to a 360 degree format.
- (double) toBearing: (double) val
{
return ( (int)([self toDegrees: val]) + 360 ) % 360; // use mod to get the degrees
}
// Calculate the bearing based off of the passed coordinates and destination.
//
- (double) calcBearingWithLatitude:(CLLocationDegrees)latSource
latitude:(CLLocationDegrees)latDest
longitude:(CLLocationDegrees)lonSrc
longitude:(CLLocationDegrees)lonDest
{
double lat1 = [self toRadian:latSource];
double lat2 = [self toRadian:latDest];
double dLon = [self toRadian:(lonDest - lonSrc)];
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
return [self toBearing:atan2(y, x)];
}
And the second.
// got this code from some forums and modified it, thanks for posting it coullis! Mostly here for reference on how to do this without sin and cos.
- (CLLocationDegrees) altCalcBearingWithLatitude:(CLLocationDegrees)latSource
latitude:(CLLocationDegrees)latDest
longitude:(CLLocationDegrees)lonSrc
longitude:(CLLocationDegrees)lonDest
{
CLLocationDegrees result;
// First You calculate Delta distances.
float dx = lonSrc - latSource;
float dy = lonDest - latDest;
// If x part is 0 we could get into division by zero problems, but in that case result can only be 90 or 270:
if (dx==0)
{
if (dy > 0)
result = 90;
else
result = 270;
}
else
{
result = [self toDegrees: atan(dy/dx)];
}
// This is only valid for two quadrants (for right side of the coordinate system) so modify result if necessary...
if (dx < 0)
result = result + 180;
// looks better if all numbers are positive (0 to 360 range)
if (result < 0)
result = result + 360;
// return our result.
return result;
}
Use this. You will have to subtract out your actual compass heading from the result of getHeadingForDirection to determine the proper relative heading. Return value is heading in radians.
-(float) angleToRadians:(float) a {
return ((a/180)*M_PI);
}
- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = [self angleToRadians:fromLoc.latitude];
float fLng = [self angleToRadians:fromLoc.longitude];
float tLat = [self angleToRadians:toLoc.latitude];
float tLng = [self angleToRadians:toLoc.longitude];
return atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));
}
Is there an algorithm to determine the minimum bounding rectangle around a set of latitude/longitude coordinates?
It is OK to assume a flat earth since the coordinates will not be too far apart. Pseudocode is OK, but if someone has done this in Objective-C, that would be even better. What I am trying to do is set the zoom level of a map based on the number of points that will be displayed on the map.
This is the method that I use in one of my apps.
- (void)centerMapAroundAnnotations
{
// if we have no annotations we can skip all of this
if ( [[myMapView annotations] count] == 0 )
return;
// then run through each annotation in the list to find the
// minimum and maximum latitude and longitude values
CLLocationCoordinate2D min;
CLLocationCoordinate2D max;
BOOL minMaxInitialized = NO;
NSUInteger numberOfValidAnnotations = 0;
for ( id<MKAnnotation> a in [myMapView annotations] )
{
// only use annotations that are of our own custom type
// in the event that the user is browsing from a location far away
// you can omit this if you want the user's location to be included in the region
if ( [a isKindOfClass: [ECAnnotation class]] )
{
// if we haven't grabbed the first good value, do so now
if ( !minMaxInitialized )
{
min = a.coordinate;
max = a.coordinate;
minMaxInitialized = YES;
}
else // otherwise compare with the current value
{
min.latitude = MIN( min.latitude, a.coordinate.latitude );
min.longitude = MIN( min.longitude, a.coordinate.longitude );
max.latitude = MAX( max.latitude, a.coordinate.latitude );
max.longitude = MAX( max.longitude, a.coordinate.longitude );
}
++numberOfValidAnnotations;
}
}
// If we don't have any valid annotations we can leave now,
// this will happen in the event that there is only the user location
if ( numberOfValidAnnotations == 0 )
return;
// Now that we have a min and max lat/lon create locations for the
// three points in a right triangle
CLLocation* locSouthWest = [[CLLocation alloc]
initWithLatitude: min.latitude
longitude: min.longitude];
CLLocation* locSouthEast = [[CLLocation alloc]
initWithLatitude: min.latitude
longitude: max.longitude];
CLLocation* locNorthEast = [[CLLocation alloc]
initWithLatitude: max.latitude
longitude: max.longitude];
// Create a region centered at the midpoint of our hypotenuse
CLLocationCoordinate2D regionCenter;
regionCenter.latitude = (min.latitude + max.latitude) / 2.0;
regionCenter.longitude = (min.longitude + max.longitude) / 2.0;
// Use the locations that we just created to calculate the distance
// between each of the points in meters.
CLLocationDistance latMeters = [locSouthEast getDistanceFrom: locNorthEast];
CLLocationDistance lonMeters = [locSouthEast getDistanceFrom: locSouthWest];
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance( regionCenter, latMeters, lonMeters );
MKCoordinateRegion fitRegion = [myMapView regionThatFits: region];
[myMapView setRegion: fitRegion animated: YES];
// Clean up
[locSouthWest release];
[locSouthEast release];
[locNorthEast release];
}
This will find the smallest latitude/longitude for your top left point
and the largest latitude/longitude for your bottom right point.
double minLat = 900;
double minLon = 900;
double maxLat = -900;
double maxLon = -900;
foreach(Point point in latloncollection )
{
minLat = Math.min( minLat, point.lat );
minLon = Math.min( minLon, point.lon );
maxLat = Math.max( maxLat, point.lat );
maxLon = Math.max( maxLon, point.lon );
}
Since the OP wants to use the bounding rectangle to set on the map, the algorithm needs to take into account the fact that latitude and longitudes are in a spherical coordinate system and the map uses a 2 dimensional coordinate system. None of the solutions posted so far take this into account and thus end up with a wrong bounding rectangle but fortunately it is quite easy to create a valid solution using the MKMapPointForCoordinate method found in this sample code from the WWDC 2013 "Whats new in MapKit" session video.
MKMapRect MapRectBoundingMapPoints(MKMapPoint points[], NSInteger pointCount){
double minX = INFINITY, maxX = -INFINITY, minY = INFINITY, maxY = -INFINITY;
NSInteger i;
for(i = -; i< pointCount; i++){
MKMapPoint p = points[i];
minX = MIN(p.x,minX);
minY = MIN(p.y,minY);
maxX = MAX(p.x,maxX);
maxY = MAX(p.y,maxY);
}
return MKMapRectMake(minX,minY,maxX - minX,maxY-minY);
}
CLLocationCoordinate2D london = CLLocationCoordinate2DMake(51.500756,-0.124661);
CLLocationCoordinate2D paris = CLLocationCoordinate2DMake(48.855228,2.34523);
MKMapPoint points[] = {MKMapPointForCoordinate(london),MKMapPointForCoordinate(paris)};
MKMapRect rect = MapRectBoundingMapPoints(points,2);
rect = MKMapRectInset(rect,
-rect.size.width * 0.05,
-rect.size.height * 0.05);
MKCoordinateRegion coordinateRegion = MKCoordinateRegionForMapRect(rect);
You can easily change the method to work on an NSArray of annotations if you prefer. E.g. here is the method I am using in my application:
- (MKCoordinateRegion)regionForAnnotations:(NSArray*)anns{
MKCoordinateRegion r;
if ([anns count] == 0){
return r;
}
double minX = INFINITY, maxX = -INFINITY, minY = INFINITY, maxY = -INFINITY;
for(id<MKAnnotation> a in anns){
MKMapPoint p = MKMapPointForCoordinate(a.coordinate);
minX = MIN(p.x,minX);
minY = MIN(p.y,minY);
maxX = MAX(p.x,maxX);
maxY = MAX(p.y,maxY);
}
MKMapRect rect = MKMapRectMake(minX,minY,maxX - minX,maxY-minY);
rect = MKMapRectInset(rect,
-rect.size.width * 0.05,
-rect.size.height * 0.05);
return MKCoordinateRegionForMapRect(rect);
}
public BoundingRectangle calculateBoundingRectangle()
{
Coordinate bndRectTopLeft = new Coordinate();
Coordinate bndRectBtRight = new Coordinate();
// Initialize bounding rectangle with first point
Coordinate firstPoint = getVertices().get(0);
bndRectTopLeft.setLongitude(firstPoint.getLongitude());
bndRectTopLeft.setLatitude(firstPoint.getLatitude());
bndRectBtRight.setLongitude(firstPoint.getLongitude());
bndRectBtRight.setLatitude(firstPoint.getLatitude());
double tempLong;
double tempLat;
// Iterate through all the points
for (int i = 0; i < getVertices().size(); i++)
{
Coordinate curNode = getVertices().get(i);
tempLong = curNode.getLongitude();
tempLat = curNode.getLatitude();
if (bndRectTopLeft.getLongitude() > tempLong) bndRectTopLeft.setLongitude(tempLong);
if (bndRectTopLeft.getLatitude() < tempLat) bndRectTopLeft.setLatitude(tempLat);
if (bndRectBtRight.getLongitude() < tempLong) bndRectBtRight.setLongitude(tempLong);
if (bndRectBtRight.getLatitude() > tempLat) bndRectBtRight.setLatitude(tempLat);
}
bndRectTopLeft.setLatitude(bndRectTopLeft.getLatitude());
bndRectBtRight.setLatitude(bndRectBtRight.getLatitude());
// Throw an error if boundaries contains poles
if ((Math.toRadians(topLeft.getLatitude()) >= (Math.PI / 2)) || (Math.toRadians(bottomRight.getLatitude()) <= -(Math.PI / 2)))
{
// Error
throw new Exception("boundaries contains poles");
}
// Now calculate bounding x coordinates
// Calculate it along latitude circle for the latitude closure to the
// pole
// (either north or south). For the other end the loitering distance
// will be slightly higher
double tempLat1 = bndRectTopLeft.getLatitude();
if (bndRectBtRight.getLatitude() < 0)
{
if (tempLat1 < (-bndRectBtRight.getLatitude()))
{
tempLat1 = (-bndRectBtRight.getLatitude());
}
}
bndRectTopLeft.setLongitude(bndRectTopLeft.getLongitude());
bndRectBtRight.setLongitude(bndRectBtRight.getLongitude());
// What if international date line is coming in between ?
// It will not affect any calculation but the range for x coordinate for the bounding rectangle will be -2.PI to +2.PI
// But the bounding rectangle should not cross itself
if ((Math.toRadians(bottomRight.getLongitude()) - Math.toRadians(topLeft.getLongitude())) >= (2 * Math.PI))
{
// Throw some error
throw new Exception("Bounding Rectangle crossing itself");
}
return new BoundingRectangle(bndRectTopLeft, bndRectBtRight);
}
This will handle exception if region crossing poles...
What #malhal wrote is correct, all the answers here are wrong and here's an example:
Take the longitudes -178, -175, +175, +178. According to the other answers, the smallest bounding box around them would be: -178 (west) : +178 (east), which is the entire world. This is not true, as the earth is round if you look from behind it you'll have a smaller bounding box of: +175 (west) : -175 (east).
This problem would occur for longitudes close to -180/+180. My brain hurts trying to think about the latitudes, but if they have a problem it's around the poles which Google Maps for example doesn't "go around", so it doesn't matter there (since its the poles).
Here is an example solution (CoffeeScript):
# This is the object that keeps the mins/maxes
corners =
latitude:
south: undefined
north: undefined
longitude:
normal:
west: undefined
east: undefined
# This keeps the min/max longitude after adding +360 to negative ones
reverse:
west: undefined
east: undefined
points.forEach (point) ->
latitude = point.latitude
longitude = point.longitude
# Setting latitude corners
corners.latitude.south = latitude if not corners.latitude.south? or latitude < corners.latitude.south
corners.latitude.north = latitude if not corners.latitude.north? or latitude > corners.latitude.north
# Setting normal longitude corners
corners.longitude.normal.west = longitude if not corners.longitude.normal.west? or longitude < corners.longitude.normal.west
corners.longitude.normal.east = longitude if not corners.longitude.normal.east? or longitude > corners.longitude.normal.east
# Setting reverse longitude corners (when looking from the other side)
longitude = if longitude < 0 then longitude + 360 else longitude
corners.longitude.reverse.west = longitude if not corners.longitude.reverse.west? or longitude < corners.longitude.reverse.west
corners.longitude.reverse.east = longitude if not corners.longitude.reverse.east? or longitude > corners.longitude.reverse.east
# Choosing the closest corners
# Extreme examples:
# Same: -174 - -178 = +186 - +182 (both eastgtive)
# Better normal: +2 - -4 < 176 - +2 (around the front)
# Better reverse: +182 - +178 < +178 - -178 (around the back)
if corners.longitude.normal.east - corners.longitude.normal.west < corners.longitude.reverse.east - corners.longitude.reverse.west
corners.longitude = corners.longitude.normal
else
corners.longitude = corners.longitude.reverse
corners.longitude.west = corners.longitude.west - 360 if corners.longitude.west > 180
corners.longitude.east = corners.longitude.east - 360 if corners.longitude.east > 180
# Now:
# SW corner at: corners.latitude.south / corners.longitude.west
# NE corner at: corners.latitude.north / corners.longitude.east
For what you want to do, you could probably just find the minimum and maximum values for Lat and Long and use those as the bounds of your rectangle. For more sophisticated solutions, see:
Calculate minimum area rectangle for a polygon
If you're in Objective-C then you might be able to use Objective-C++ instead, in which case you can use the STL to do a lot of the heavy lifting for you:
#include <vector>
#include <algorithm>
std::vector<float> latitude_set;
std::vector<float> longitude_set;
latitude_set.push_back(latitude_a);
latitude_set.push_back(latitude_b);
latitude_set.push_back(latitude_c);
latitude_set.push_back(latitude_d);
latitude_set.push_back(latitude_e);
longitude_set.push_back(longitude_a);
longitude_set.push_back(longitude_b);
longitude_set.push_back(longitude_c);
longitude_set.push_back(longitude_d);
longitude_set.push_back(longitude_e);
float min_latitude = *std::min_element(latitude_set.begin(), latitude_set.end());
float max_latitude = *std::max_element(latitude_set.begin(), latitude_set.end());
float min_longitude = *std::min_element(longitude_set.begin(), longitude_set.end());
float max_longitude = *std::max_element(longitude_set.begin(), longitude_set.end());
All you need to do is get the left-most, top-most, right-most, and bottom-most values. You could accomplish this pretty easily by sorting, and so long as the set isn't too big, it wouldn't be very expensive.
If you give your lat/long class methods called compareLatitude: and compareLongitude:, it'd be even easier.
CGFloat north, west, east, south;
[latLongCollection sortUsingSelector:#selector(compareLongitude:)];
west = [[latLongCollection objectAtIndex:0] longitude];
east = [[latLongCollection lastObject] longitude];
[latLongCollection sortUsingSelector:#selector(compareLatitude:)];
south = [[latLongCollection objectAtIndex:0] latitude];
north = [[latLongCollection lastObject] latitude];
Something like that should work, assuming your collection of coordinates is an NSMutableArray.